0

Let me give you an example;

String[] one = {"one", "two"};
String[] two = {"bob", "lol", "hi"};
List<String[]> list = new ArrayList<String[]>();
list.add(one);
list.add(two);

Now, I want to get the 2nd string array (which is 'two') in list. I do this by:

list.get(2);

But, say if I wanted to get the 2nd element in the two String array in List ( Basically I want to get the string "lol" from list->two->lol).

Is this how you do it:

list.get(2).get(2) 
2
  • That should be list.get(1)[1], since list indices start at 0, not 1, list.get(1) is an array and array indices start at 0, not 1. Arrays don't have a .get() method. Commented Mar 18, 2014 at 6:33
  • 1
    ... and list.get(1)... Commented Mar 18, 2014 at 6:34

2 Answers 2

3

Indices in Java (and in most programming languages) starts with 0, so if you want to access to the second element you must use the index 1:

list.get(0)[1];

Note that

list.get(0)

will return the first String[] array, and to access to an element of an array you have to use the syntax:

someArray[index]
Sign up to request clarification or add additional context in comments.

1 Comment

@EbadSaghar this answer posted on 06:33:36, 3sec after my answer (06:33:33) :P but its okay, this is more perfect
0

Array starts with index 0. so you have to use list.get(i)[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.