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

試著忘記壹切 提交于 2020-01-02 11:23:59

问题


I have an array (Arr). I fill points1 and points2 with values.

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?

And how can I see the points[][] array like var_dump in PHP? How list the array? var_dump example:

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

回答1:


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



回答2:


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.

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.

You could make, per example an ArrayList of '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.

Once you're using a Java Collection, the clearest way to print these is just looping over it , with something like

for(Arr a : list){
  Log.d("my_tag", "My list contains"+a.toString());
}



回答3:


Instead of 2D array, you can use 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:

System.out.println(points);



回答4:


You need to implement a toString() method in your class Arr, and then call

Log.i("mytag", Arrays.toString(points1));

as explained here how see an array in logcat for android



来源:https://stackoverflow.com/questions/15134193/java-add-values-to-two-dimensional-array-and-how-see-the-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!