我如何访问列表的数组元素 [英] How I can access array elements of List

查看:48
本文介绍了我如何访问列表的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:

我有一个用不同值填充的列表Java.例如,我有:

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);

,我也有其他值.现在,我只想在此列表中的第一个字段中进行搜索.例如,我想要A的indexOf.我尝试编写以下代码:

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");

但是我得到-1作为回报.我想知道在加载数组时如何访问列表的字段.

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.

推荐答案

我想要A的indexOf.

I want the A's indexOf.

不,您无法获得它,因为它是数组中的一个元素.数组没有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)为您提供第一个插入的数组,[0]为您提供该数组中的第一个positiend元素.

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

如果要在数组中查找 A 的位置,请使用 l.get(0)从列表中访问该数组,然后遍历该数组以获取 A 的位置.

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;
            }
        }
    }

注意:请以以下示例代码为例,并添加对null,重复项等的检查...

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

这篇关于我如何访问列表的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆