FranXh
FranXh

Reputation: 4771

Adding element in two dimensional ArrayList

I know that for arrays you can add an element in a two dimensional array this way:

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

How can I do the same thing with ArrayList?

Upvotes: 8

Views: 107566

Answers (6)

flexJavaMysql
flexJavaMysql

Reputation: 74

 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 element must be initialised

These elements contain elements of type String

To get back the String "String" in the 3-line example, you would use

String getValue = data.get(0).get(0);

Upvotes: 2

concrain
concrain

Reputation: 1

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);

Simple for loop to add data to a multidimensional Array.
For every outer index you need to add

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

then increment the outer index, and reset the inner index.
That would 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));
}

Upvotes: 0

sudesh
sudesh

Reputation: 43

ArrayList<ArrayList<Integer>> FLCP = new ArrayList<ArrayList<Integer>>();
FLCP.add(new ArrayList<Integer>());
FLCP.get(0).add(new Integer(0));

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

Some additional notes; after reading other answers and comments:

1> Each element must be instantiated; initialization is different from instantiation (refer to flexJavaMysql's answer)

2> In Java, 2-dimensional arrays do exist; C# doesn't have 2D arrays (refer to JB Nizet's answer)

Upvotes: 1

Aiden
Aiden

Reputation: 335

the way i found best and convinient for me was to declare ur 2d arrayList and then also a nornal mono-dimension array.

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

then fill the '1D'array list and later add the 1D to the 2D array list.

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

this will work as long as your problem is simply to add to elements to the list. if u want to add them to specific positions in the list, the the .get().set(); is what u wanna stick to.

Upvotes: 1

Joey
Joey

Reputation: 354526

myList.get(0).set(1, 17);

maybe?

This assumes a nested ArrayList, i.e.

ArrayList<ArrayList<Integer>> myList;

And to pick on your choice of words: This assigns a value to a specific place in the inner list, it doesn't add one. But so does your code example, as arrays are of a fixed size, so you have to create them in the right size and then assign values to the individual element slots.

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

Upvotes: 11

JB Nizet
JB Nizet

Reputation: 691735

outerList.get(0).set(1, 17);

with outerList being a List<List<Integer>>.

Remember that 2-dimensional arrays don't exist. They're in fact arrays or arrays.

Upvotes: 3

Related Questions