问题
I have created a LinkedList
of String
arrays:
public static void main(String[] args){
String[] one = new String[10];
String[] two = new String[10];
String[] three = new String[10];
LinkedList<String[]> myList = new LinkedList<String[]>();
myList.add(one);
myList.add(two);
myList.add(three);
}
My question is how do I now access an element of one of the arrays. For instance, I can access (or in the example print) the entire array element via:
System.out.println(myList.get(0));
But how do i access the first element in the String
array that is owned by myList.get(0)
? I tried everything like myList.get(0).[0]
, but I can't figure out how to access an element in an array, when the array is an element of a linked list.
回答1:
array.get(0)[index];
That should do the trick.
array.get(0)
returns an array, all you have to do is add your index after it (no dot to separate).
Just the same way you would use one[index]
.
来源:https://stackoverflow.com/questions/20361899/how-do-i-reference-an-array-element-in-a-list-of-arrays