How I can access array elements of List

浪尽此生 提交于 2021-02-16 22:45:20

问题


I have a question:

I have a List Java that I have populated with different values. For example, I have:

List<String[]> l = new ArrayList();
String[] lis = new String[3];
lis[0] = "A";
lis[1] = "B";
lis[2] = "C";
l.add(lis);

and I have other values too. Now, I want to get the search in this list only the first field. For example, I want the A's indexOf. I have tried to write this code:

int index = l.indexOf("A");

but I get -1 as return. I would like to know how I can access to a field of the list, when I load an array.


回答1:


I want the A's indexOf.

No, you can't get it, since it is an element inside array. Array doesn't have indexOf method. List have that method.

I would like to know how I can access to a field of the list, when I load an array.

String firstElem= l.get(0)[0]; 

get(0) gives you the first inserted array and [0] gives you the first positiend element in that array.

If you are looking for position of A in array, access the array from list with l.get(0) and iterate over the array you got to get the position of A.

Update after comment: If you have no idea of indexes you have

for (int i = 0; i < l.size(); i++) {
        for (int j = 0; j < l.get(i).length; j++) {
            if( l.get(i)[j].equalsIgnoreCase("A")){
                return j;
            }
        }
    }

Note: Consider that as a example code and add checks for null's ,duplicates etc ...




回答2:


You want do this :

List<String> l = new ArrayList();
String[] lis = new String[3];
lis[0] = "A";
lis[1] = "B";
lis[2] = "C";
l.addAll(lis);

Or you do not have to use array at all

List<String> l = new ArrayList();    
l.add("A");
l.add("B");
l.add("C");

In your case, you have list of arrays, not list of strings.




回答3:


after you l.add(lis); your list l has only one single element: String[] lis. not n elements of the array

you check if a String "A" in your list by indexOf, it returns -1, says, no I don't have that element.

You may want to check Arrays.asList() method, It converts an array into a List. I guess that is what you want.




回答4:


 List<String[]> l = new ArrayList<>();
 String[] lis = new String[3];
 lis[0] = "A";
 lis[1] = "B";
 lis[2] = "C";
 l.add(lis); //  now your list have only one element and it's index is 0

You list don't have "A". So l.indexOf("A") will return -1

Where is "A" now?

l.get(0)[0] // l.get(0) is lis array. Then 0th element of lis is A

If you want to get index in this way you can use

String[] lis = new String[3];
lis[0] = "A";
lis[1] = "B";
lis[2] = "C";        

int index= Arrays.asList(lis).indexOf("A");
System.out.println(index);

Out put:

0



回答5:


Your List contains String arrays (String[]). I think what you meant to do was to create a List<String>.

If you truly want a List<String[]>, then your code would look something like this:

for(int i = 0; i < list.size(); i++)
{
    String[] array = list.get(i);
    for(String s : array)
    {
        if("A".equals(s))
        {
            return i;
        }
    }
}


来源:https://stackoverflow.com/questions/26057148/how-i-can-access-array-elements-of-list

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