Home Update array element in arraylist of arrays

# 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 IT = new ArrayList(); 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#
 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"; 
 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