Java - 将值添加到二维数组以及如何查看数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15134193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Add values to two-dimensional Array and how see the array?
提问by Johnny
I have an array (Arr). I fill points1 and points2 with values.
我有一个数组(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;
}
This works, but what is, when I have five of them or more (points1, points2, points3...) How can I make "public Arr points[][]"? And then fill and get the values with e.g. points[0][22].pointX or points[1][10].text?
这是有效的,但什么是,当我有五个或更多(points1,points2,points3 ...)时,我该如何制作“public Arr points[][]”?然后用例如points[0][22].pointX 或points[1][10].text 填充并获取值?
And how can I see the points[][] array like var_dump in PHP? How list the array? var_dump example:
以及如何在 PHP 中看到类似 var_dump 的 points[][] 数组?如何列出数组?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"
}
}
采纳答案by Bojan Kogoj
This is a working example
这是一个工作示例
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;
}
回答by Stefan de Bruijn
I'm not completely clear on what you're trying to achieve, but Java isn't PHP. Java has an extensive set of Collections which will give you much better coverage then simple array's.
我不完全清楚您要实现的目标,但 Java 不是 PHP。Java 有一组广泛的集合,它可以为您提供比简单数组更好的覆盖范围。
As I'm not completely sure what you're trying to do, I can't really tell you what collections could do the job, but you could read up on things like Maps and ArrayList.
由于我不完全确定您要做什么,我无法真正告诉您哪些集合可以完成这项工作,但您可以阅读 Maps 和 ArrayList 之类的内容。
You could make, per example an ArrayList of 'Arr's like
例如,您可以制作一个 ArrayList 的 'Arr's like
ArrayList<Arr> list = new ArrayList<Arr>();
Similarly you can do ArrayList<Arr, Arr>
, or start working with Maps if you want to store your object Arr together with a key to find it with.
同样ArrayList<Arr, Arr>
,如果您想将对象 Arr 与查找它的键一起存储,您可以执行或开始使用 Maps。
Once you're using a Java Collection, the clearest way to print these is just looping over it , with something like
一旦您使用了 Java 集合,打印这些最清晰的方法就是循环遍历它,例如
for(Arr a : list){
Log.d("my_tag", "My list contains"+a.toString());
}
回答by Azodious
Instead of 2D array, you can use ArrayList
.
您可以使用ArrayList
.
ArrayList<Arr> points = new ArrayList<Arr>();
Now, access an index as following:
现在,访问索引如下:
points.get(22).pointX OR points.get(10).text
Try to print the arraylist points
, to see a near about string output:
尝试打印 arraylist points
,以查看接近的字符串输出:
System.out.println(points);
回答by Premsuraj
You need to implement a toString() method in your class Arr, and then call
你需要在你的类 Arr 中实现一个 toString() 方法,然后调用
Log.i("mytag", Arrays.toString(points1));
as explained here how see an array in logcat for android