gpt4 book ai didi

Java - 将值添加到二维数组以及如何查看数组?

转载 作者:行者123 更新时间:2023-11-30 11:35:25 25 4
gpt4 key购买 nike

我有一个数组 (Arr)。我用值填充 points1 和 points2。

public class Arr {
float pointX;
float pointY;
boolean yesNo;
String text;
}

public Arr points1[];
public Arr points2[];

points1 = new Arr[100];
for (int i = 0; i < 100; i++) points1[i]= new Arr();

for (int j = 0; j < 100; j++) {
points1[j].pointX = getFloat;
points1[j].pointY = getFloat;
points1[j].yesNo = getBoolean;
points1[j].text = getText;
}

points2 = new Arr[100];
for (int x = 0; x < 100; x++) points2[x]= new Arr();

for (int y = 0; y < 100; y++) {
points2[y].pointX = getFloat;
points2[y].pointY = getFloat;
points2[y].yesNo = getBoolean;
points2[y].text = getText;
}

这行得通,但是当我有五个或更多时(points1,points2,points3 ...)如何制作“public Arr points[][]”?然后填写并获取值,例如points[0][22].pointX 还是 points[1][10].text?

我如何在 PHP 中看到 points[][] 数组,如 var_dump?如何列出数组?var_dump 示例:

array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}

最佳答案

这是一个有效的例子

package com.stackoverflow.q15134193;

public class Test1 {
public static Arr[][] points;
static float getFloat = 1;
static boolean getBoolean = true;
static String getText = "hi";

public static void main(String[] args) {

points = new Arr[100][];
for (int i = 0; i < points.length; i++) {
points[i] = new Arr[100];
for (int j = 0; j < points[i].length; j++)
points[i][j] = new Arr();
}

for (int i = 0; i < points.length; i++) {
for (int j = 0; j < points[i].length; j++) {
points[i][j].pointX = getFloat;
points[i][j].pointY = getFloat;
points[i][j].yesNo = getBoolean;
points[i][j].text = getText;
}
}

for (int i = 0; i < points.length; i++)
for (int j = 0; j < points[i].length; j++)
System.out.println("X: " + points[i][j].pointX + " Y: "
+ points[i][j].pointY + " YesNo: "
+ points[i][j].yesNo
+ " text: "+ points[i][j].text);
}
}

class Arr {
public float pointX;
public float pointY;
public boolean yesNo;
public String text;
}

关于Java - 将值添加到二维数组以及如何查看数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134193/

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