java 2 维数组,如何使作为Java的2维数组ArrayList的工作?

I want to make arrayList object in java that work as two dimentional array. My question is how can we access value from specific dimention from arrayList.

in two dimentional array, if i want to access value then it can be as m[i][j].

But in arraylist how can i do that ?

解决方案

List> list = new ArrayList>();

list.add(new ArrayList());

list.add(new ArrayList());

list.get(0).add(5);

list.get(1).add(6);

for(List listiter : list)

{

for(Integer integer : listiter)

{

System.out.println("" + integer);

}

}

This way you can get the items like

list.get(1).get(0); //second dimension list -> integer

EDIT:

Although it is true that you can use a Map if you are trying to use numeric indices for example for each list, like so:

Map> map = new HashMap>();

map.put(0, new ArrayList());

map.put(5, new ArrayList());

map.get(0).add(new YourObject("Hello"));

map.get(5).add(new YourObject("World"));

for(Integer integer : map.keySet())

{

for(YourObject yourObject : map.get(integer))

{

yourObject.print(); //example method

System.out.println(" ");

}

}

Although even then the accessing of Lists would be the same as before,

map.get(0).get(1); //List -> value at index

Obviously you don't need to use Integers as the generic type parameter, that's just a placeholder type.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值