8

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?

3
  • @Joey: what do you doubt? Please see code in answer as it uses get(...).get(...). How does this disallow my comment above? Commented Mar 3, 2012 at 23:10
  • 1
    Whatever get(1) returns, assigning 17 to it won't add it to the List - and won't compile anyway, surely? Commented Mar 3, 2012 at 23:16
  • 1
    @Joey: Yep, you're right and i wasn't thinking. Must not be enough blood in my alcohol system right now. Commented Mar 3, 2012 at 23:17

6 Answers 6

11
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm so glad James Gosling scorned operator overloads.
...where get(0) returns a List, of course. Also noting that 17 is autoboxed to new Integer(17)
3
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.

1 Comment

So if I had a 10x grid (2D array) if i wanted to add to the last item would it be myList.get(9).set(9, <my Int>); ?
3
 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);

Comments

1

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.

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.