Adding an item to a two-dimensional ArrayList array

I know that for arrays you can add an element to a two-dimensional array as follows:

array[0][1] = 17; //just an example 

How can I do the same with an ArrayList?

+6
source share
6 answers
 myList.get(0).set(1, 17); 

may be?

This assumes a nested ArrayList , i.e.

 ArrayList<ArrayList<Integer>> myList; 

And choose your choice of words: this assigns a value to a specific place in the internal list, it does not add it. But also your code example, since arrays have a fixed size, so you need to create them in the right size, and then assign values ​​to individual element slots.

If you really want to add an element, then of course it is .add(17) , but that is not what your code did, so I went with the code above.

+6
source
 outerList.get(0).set(1, 17); 

where outerList is a List<List<Integer>> .

Remember that two-dimensional arrays do not exist. They are actually arrays or arrays.

+1
source
  ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); data.add(new ArrayList<String>()); data.get(0).add("String"); 

ArrayList<ArrayList<String>> contains elements of type ArrayList<String>

Each item must be initialized.

These elements contain elements of type String

To return a String "String" in a 3-string example, you should use

 String getValue = data.get(0).get(0); 
+1
source
 ArrayList<ArrayList<Integer>> FLCP = new ArrayList<ArrayList<Integer>>(); FLCP.add(new ArrayList<Integer>()); FLCP.get(0).add(new Integer(0)); 

Each item must be created. Here, the outer ArrayList has an ArrayList element, and first you need to add an element to reference it using the get method.

Some additional notes; after reading other answers and comments:

1> Each item must be created; initialization differs from instance (see flexJavaMysql answer )

2> In Java, there are two-dimensional arrays; C # doesn't have 2D arrays (see JB Nizet answer )

+1
source

the way I found the best and most convenient for me was to declare ur 2d arrayList and then also the anomalous monomeric size.

 ArrayList<ArrayList<String>> 2darraylist = new ArrayList<>(); ArrayList<String> 1darraylist=new ArrayList<>(); 

then fill out the 1D'array list, and then add 1D to the list of 2D arrays.

 1darraylist.add("string data"); 2darraylist.add(idarraylist); 

this will work as long as your problem is simply adding items to the list. if you want to add them to certain positions in the list, then .get (). set (); this is what you want to stick with.

0
source
 String[] myList = {"a","b","c","d"}; ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); data.add(new ArrayList<String>()); int outerIndex =0; int innerIndex =0; for (int i =0; i<list.length; i++) { data.get(outerIndex).add(innerIndex, list[i]); innerIndex++; } System.out.println(data); 

A simple loop to add data to a multidimensional array.
For each external index you need to add

 data.add(new ArrayList<String>()); 

then increase the outer index and reset the inner index.
It will look something like this.

 public static String[] myList = {"a", "b","-","c","d","-","e","f","-"}; public static ArrayList<ArrayList<String>> splitList(String[] list) { ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); data.add(new ArrayList<String>()); int outerIndex =0; int innerIndex =0; for (int i=0; i<list.length; i++) { System.out.println("will add: " + list[i]); if(!list[i].contains("-")) { System.out.println("outerIndex: " + outerIndex +" innerIndex: "+ innerIndex); data.get(outerIndex).add(innerIndex, list[i]); innerIndex++; } else { outerIndex++; // will move to next outerIndex innerIndex = 0; // reset or you will be out of bounds if (i != list.length-1) { data.add(new ArrayList<String>()); // create an new outer index until your list is empty } } } return data; } public static void main(String[] args) { System.out.println(splitList(myList)); } 
0
source

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


All Articles