我有一个数组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…)如何使"公共Arr点[][]"?然后填充并获取值,例如points[0][22]。pointX或points[1][10].text?
我怎么能看到点[][]数组像var_dump在PHP?如何列出数组?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不是PHP。Java有一个广泛的集合集,它将为您提供比简单数组更好的覆盖率。
因为我不完全确定你要做什么,所以我不能告诉你集合可以做什么工作,但是你可以阅读像map和ArrayList这样的东西。
你可以创建一个数组列表,比如
ArrayList<Arr> list = new ArrayList<Arr>();
类似地,您可以执行ArrayList<Arr, Arr>
,或者开始使用Maps,如果您想将对象Arr与查找它的键一起存储。
一旦你使用了Java Collection,最清晰的打印方式就是循环遍历它,比如
for(Arr a : list){
Log.d("my_tag", "My list contains"+a.toString());
}
可以用ArrayList
代替2D数组
ArrayList<Arr> points = new ArrayList<Arr>();
现在,按如下方式访问索引:
points.get(22).pointX OR points.get(10).text
尝试打印数组列表points
,以查看近约字符串输出:
System.out.println(points);
您需要在类Arr中实现toString()方法,然后调用
Log.i("mytag", Arrays.toString(points1));