How to add a row to a specific index in an array of strings

I have a String array as shown below:

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

I need to add some more values ​​to this array in the following state, for example 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.

Is there a way to add these values ​​based on the index or in any way without creating, for example, the example above? (Is it possible?)

+4
source share
7 answers

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" }; 

The array property "after initializing the array after that you cannot resize the array" *
so in your case you have to initialize a new array ....
Or you can use an ArrayList and with the add(index, value) method you can add your value to the required position ..
But implicit, it also uses the Concept of Dynamic Array..

+5
source

I may not have understood your question, but you can use ArrayList<String> instead of simple arrays. You can do something like this:

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

So, you basically add two new elements at positions 1 and 2 to the ArrayList<String> headers.

And no, you cannot do this with regular arrays.

+5
source

No, you can’t. Arrays are not dynamic. You need to create a new array. The size must be known before the array is created.

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

To add them based on the index, you really need to create a new array and display all the value from the old array and add new values ​​at runtime.

It would be much simpler if you would use an ArrayList that has an insert method that can insert new values ​​at any index in the list

Example:

 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"); 
+3
source

You can save your lines in an ArrayList<String> , and then use the .add(index, text) method

+2
source

It is better to use List<String> and just use the add() method.

+2
source

You can use ArrayList as follows:

 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"); 

It's good that with ArrayList you can use Iterator and perform various operations, such as searching for a specific record.

+2
source

Source: https://habr.com/ru/post/1386454/


All Articles