简体   繁体   English

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

[英]Java - Add values to two-dimensional Array and how see the array?

I have an array (Arr). 我有一个数组(Arr)。 I fill points1 and points2 with values. 我用值填充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[][]"? 这行得通,但是当我有五个或更多(points1,points2,points3 ...)时,该怎么做?如何设置“ public Arr points [] []”? And then fill and get the values with eg points[0][22].pointX or points[1][10].text? 然后用点[0] [22] .pointX或点[1] [10] .text填充并获取值。

And how can I see the points[][] array like var_dump in PHP? 以及如何在PHP中看到points [] []数组,例如var_dump? How list the array? 如何列出数组? var_dump example: 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"
  }
}

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;
}

I'm not completely clear on what you're trying to achieve, but Java isn't PHP. 对于您要实现的目标,我还不太清楚,但Java不是PHP。 Java has an extensive set of Collections which will give you much better coverage then simple array's. Java具有广泛的Collections集,它将为您提供比简单数组更好的覆盖率。

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 例如,您可以将'Arr'的ArrayList做成

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. 同样,如果您想将对象Arr与用于查找它的键一起存储ArrayList<Arr, Arr>可以执行ArrayList<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());
}

Instead of 2D array, you can use ArrayList . 代替2D数组,可以使用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);

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 如此处所述, 如何在Android的logcat中查看数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM