How do I reference an Array element in a List of Arrays?

梦想与她 提交于 2021-02-07 19:36:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!