gpt4 book ai didi

java - java中从ArrayList中获取值

转载 作者:行者123 更新时间:2023-12-01 16:36:02 24 4
gpt4 key购买 nike

我向drawPoints[][]添加了一些值,然后创建了一个ArrayList对象点

ArrayList points=new ArrayList();
points.add(drawPoints);

现在我想从点中检索drawPoints的值,我该怎么做?

最佳答案

您需要使用

访问 ArrayList 中的第一个 drawPoints
int[][] tmp = (int[][]) points.get(0);

然后您将能够获取值,例如

tmp[0][1];

下面是一个打印存储在 ArrayList 中的两个二维数组的值的程序小示例:

int[][] drawPoints = new int[2][2];
int[][] drawPoints2 = new int[2][2];
drawPoints[0][0] = 1;
drawPoints[0][1] = 2;
drawPoints[1][0] = 3;
drawPoints[1][1] = 4;
drawPoints2[0][0] = 5;
drawPoints2[0][1] = 6;
drawPoints2[1][0] = 7;
drawPoints2[1][1] = 8;
ArrayList<Object> points=new ArrayList<Object>();
points.add(drawPoints);
points.add(drawPoints2);

for(Object tab : points){
int[][] tmp = (int[][]) tab;
for(int i=0;i < tmp.length;i++){
for(int j=0;j<tmp[i].length;j++){
System.out.println(tmp[i][j]);
}
}
System.out.println("------------");
}

关于java - java中从ArrayList中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8992579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com