java数组访问所有元素,我如何访问列表的数组元素

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 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值