2

I am using an ArrayList of to create lists. For example:

Index 0: 1 3
Index 1: 4 5
Index 2: 1 3 7

How can I access the second element of the first index of the ArrayList? Couldn't find the answer on Google, so I asked here.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
Marian Pavel
  • 2,726
  • 8
  • 29
  • 65
  • 1
    Why not just use a 2d array? – Adam Jan 08 '15 at 16:21
  • 1
    I googled "ArrayList of integers" and the first result was http://stackoverflow.com/questions/14421943/java-arraylist-for-integers – Hanlet Escaño Jan 08 '15 at 16:22
  • Can't find on google ?? – Abdelrahman Elkady Jan 08 '15 at 16:22
  • 1
    Show us some code, because a `ArrayList` isn't an array of `Integer`(s). – Elliott Frisch Jan 08 '15 at 16:23
  • I mean if its a true Arraylist. You could use `myList.get(0)` which would return the first index, but past that I don't know :P – Adam Jan 08 '15 at 16:23
  • 2
    I found the correct answer on @Fev, I don't use a 2d Array because it's harder to add elements dinamically. ArrayList of integer[] it's much easier. – Marian Pavel Jan 08 '15 at 16:27
  • @PavelMarian Sounds like you should be using a `List> al = new ArrayList<>();` why use one dynamic collection and a fixed width object? – Elliott Frisch Jan 08 '15 at 16:29
  • 1
    @HanletEscaño That looks like an accident. This is about an "ArrayList of arrays of integers", not "ArrayList of integers". But the second phrase happened to work because someone put the wrong title on their question. My point is: don't assume that everyone should just be able to find things with Google. It's not always easy to guess what search terms to put in. – ajb Jan 08 '15 at 16:32

4 Answers4

4
yourList.get(0)[1]; // that's it !!

If you want to iterate over it :

for (Integer[] outer : yourList) {
  for(Integer inner : outer) {
    System.out.println(inner);
  }
}
David Conrad
  • 15,432
  • 2
  • 42
  • 54
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
  • If it was so simple why I couldn't find it over the internet, thanks a lot.Gonna try it ! Mark answer after time pass. – Marian Pavel Jan 08 '15 at 16:26
2

By your question, I am guessing you have something like this?

List<Integer[]> list = new ArrayList<Integer[]>();
Integer[] a1 = {1,3};
Integer[] a2 = {4,5};
Integer[] a3 = {1,3,7};

list.add(a1);
list.add(a2);
list.add(a3);

Then all you need to do is simply call:

Integer result = list.get(0)[1];

The get(0) pulls the first Integer[] out of the list, then to get the second element, you use [1]

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
0

where do you see the exception? have you tried this?

    List<Integer[]> list = new ArrayList<Integer[]>(3);
    Integer[] a1 = {1,3};
    Integer[] a2 = {4,5};
    Integer[] a3 = {1,3,7};

    list.add(a1);
    list.add(a2);
    list.add(a3);

    Integer result = list.get(0)[1];

There aren't any exception. The arraylist have three elementes because you have three elemente (a1,a2,a3),

nole
  • 1,422
  • 4
  • 20
  • 32
-1

You have List<Integer[]> l = new ArrayList<Integer[]>(3);

if you want the second element of the first index:

l.get(0)[1].
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
nole
  • 1,422
  • 4
  • 20
  • 32