简体   繁体   English

如何在字符串数组中的特定索引处追加String

[英]How to append String at particular index in an array of Strings

I have a String array like below: 我有一个如下所示的String数组:

String[] titles = new String[] { "Booking", "Entry", "Type",
                    "Duration", "Status", "Contract ID",
                    "Capacity", "Start Day", "End Day", "Source Ref" };

I need to append, some more values to this array at later state, like below: 我需要在以后的状态下向这个数组附加一些值,如下所示:

titles = new String[] { "Booking", "Shipper", "Booking Quantity", "Entry", 
                        "Type", "Duration", "Status", 
                        "Contract ID", "Capacity", "Start Day", 
                        "End Day", "Source Ref" };

here I added "Shipper", "Booking Quantity" to the first array. 在这里,我将"Shipper", "Booking Quantity"到第一个阵列。

Is there any way to append those values based on index or any way, without creating like above example? 有没有办法基于索引或任何方式附加这些值,而不创建像上面的例子? (Is it possible?) (可能吗?)

Maybe I didn't get right your question, but you can use ArrayList<String> instead of simple arrays. 也许我没有解决你的问题,但你可以使用ArrayList<String>而不是简单的数组。 You can do something like this: 你可以这样做:

titles.add(1, "Shipper");
titles.add(2, "Book Quantity");

So you basically add to the ArrayList<String> of titles the two new elements at position 1 and 2. 所以你基本上将标题的ArrayList<String>添加到位置1和2的两个新元素。

And no, you can't do that with normal arrays. 不,你不能用普通数组做到这一点。

when you initialize an array like this 当你初始化这样的数组时

String[] titles = new String[] { "Booking", "Entry", "Type",
                "Duration", "Status", "Contract ID",
                "Capacity", "Start Day", "End Day", "Source Ref" };

Property of array "once you initialize the array after that you can't change the size of array"* 数组的属性“一旦你初始化数组之后就不能改变数组的大小”*
so in your case you must initialize new array .... 所以在你的情况下你必须初始化新的数组....
Or you can use ArrayList and with its add(index, value) method you can add your value at your required position .. 或者您可以使用ArrayList及其add(index, value)方法,您可以在所需位置添加值。
But implicit it also use Concept of Dynamic Array.. 但隐含它也使用Concept of Dynamic Array..

No you can't. 不,你不能。 Arrays are not dynamic. 数组不是动态的。 You need to create a new array. 您需要创建一个新数组。 Size must be know before creating the array. 在创建数组之前必须知道大小。

String[] strings = new String[titles.length + 2];
strings[0] = "Booking";
strings[1] = "Shipper";
//...

To add them based on index you would actually need to create a new array and map all of the value from the old array and add the new values in as you go. 要根据索引添加它们,您实际上需要创建一个新数组并映射旧数组中的所有值,并随时添加新值。

It would be much easier if you were using an ArrayList , which has an insert method that can insert new values at any index in the List 如果您使用的是ArrayList ,它会更容易,它具有可以在List中的任何索引处插入新值的insert方法

ex: 例如:

ArrayList<String> myList = new ArrayList<>();
myList.add("Booking");
myList.add("Entry");
myList.add("Type");
myList.add("Duration");
myList.add("Status");
myList.add("Contract ID"); 
myList.add("Capacity");
myList.add("Start Day");
myList.add("End Day");
myList.add("Source Ref");

//insertion 
myList.add(1, "Shipper");
mylist.add(2, "Booking Quantity");

您可以将字符串保存在ArrayList<String> ,然后使用.add(index, text)方法

最好使用List<String>并使用add()方法。

You can use ArrayList as follows: 您可以使用ArrayList,如下所示:

ArrayList titleList = new ArrayList<String>();
titleList.add("Shipper");
titleList.add("Booking");
titleList.add("Entry");
titleList.add("Type");
titleList.add("Duration");
titleList.add("Status");
titleList.add("Contract");
titleList.add("ID");
//....
titleList.add(1,"Shipper")
titleList.add(2,"Booking quality");

The good thing about using ArrayList is you can use Iterator and can perform various operations like searching particular entry. 使用ArrayList的好处是您可以使用Iterator并执行各种操作,例如搜索特定条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串插入字符串数组中的特定索引并在此之后移动字符串? - How to Insert string to a particular index in string array and shifting strings after that? 给定一个字符串数组,如何找出特定字符串的索引? - Given an array of Strings, how to find out the index of a particular String? 根据特定的字符串位置将字符串追加到文件 - Append strings to a file based on a particular string position 如何在字符串数组中找到给定String的索引? - How to find an index of given String in array of strings? 按字符串或索引的一部分对字符串数组进行排序? - Sort an array of strings by a part of the string or index? 如何在Java中的特定索引处拆分字符串? - How to split a string in Java at a particular index? 如何在文本索引的特定索引处设置字符串 - How to set string at a particular index to a textview 对于给定的字符串数组,如何在字符串的索引处打印该字符串的字符 - How would you print out the character of that string at the index of the string for a given array of Strings 获取数组中字符串的索引,并在其他数组中删除这些索引处的字符串 - get index of string in Array and delete in an other Array the strings at these indexes 如何将字符串转换为字符串数组? - How to turn a string into an array of strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM