Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a String array like below:

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.

Is there any way to append those values based on index or any way, without creating like above example? (Is it possible?)

like image 658
Chandra Sekhar Avatar asked Nov 30 '22 03:11

Chandra Sekhar


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

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 ..
But implicit it also use Concept of Dynamic Array..

like image 172
Sumit Singh Avatar answered Dec 01 '22 15:12

Sumit Singh