Home Update array element in arraylist of arrays
Reply: 2

Update array element in arraylist of arrays

Moh.Ali
1#
Moh.Ali Published in 2018-02-12 16:40:31Z

I need to update an element of array inside arraylist of arrays

ArrayList<Object[]> IT = new ArrayList<Object[]>();
String[] arr1 = {"1" , "2" , "3"};    
String[] arr2 = {"4" , "5" , "6"}; 
String[] arr3 = {"7" , "8" , "9"};
IT.add(arr1); 
IT.add(arr2);
IT.add(arr3);

I need to update elements of these internal arrays.

chŝdk
2#
chŝdk Reply to 2018-02-12 16:53:09Z

Like arrays you can access the ArrayList elements by their index, you can use get(index) method, so what you need to do is:

First get the String[] from the list with its index:

IT.get(0)

Then get the relevant element to update in this Array with its index as well:

IT.get(0)[1] = "SDSDSD";
mav3n
3#
mav3n Reply to 2018-02-12 17:01:20Z

Its same as 2D array, the only difference is 1 of the dimension is ArrayList instead of Array. You need to do it in steps in the order of there definition.

First you need to access the ArrayList by using IT.get(rowIndex), this will return you an array, which you can access using the array syntax for accessing like arr[columnIndex]. Since the arr here is IT.get(rowIndex) you access it by

IT.get(rowIndex)[columnIndex]

e.g.

System.out.println(IT.get(1)[1]); //  Output : 5 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.357578 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO