简体   繁体   中英

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

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 eg 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"
  }
}

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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