I have a question:
I have a List Java that I have populated with different values. For example, I have:
List 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.
解决方案
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 ...