[英]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.