I have string containing n elements.
I want to insert a automatically incremented number to the first position.
E.g Data
66,45,34,23,39,83
64,46,332,73,39,33
54,76,32,23,96,42
I am spliting to string to array with split char ','
I want resultant array with a incremented number a first position
1,66,45,34,23,39,83
2,64,46,332,73,39,33
3,54,76,32,23,96,42
Please suggest how can I do it.
Thanks
By using ArrayList as intermediate storage: Create an ArrayList with the original array, using asList() method. Simply add the required element in the list using add() method. Convert the list to an array using toArray() method.
Since Java arrays hold a fixed number of values, you need to create a new array with a length of 5 in this case. A better solution would be to use an ArrayList and simply add strings to the array.
You can't with an array, you need to use a List<string>
instead.
For example:
List<string> words = new string[] { "Hello", "world" }.ToList();
words.Insert(0, "Well");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With