Java - Add values to two-dimensional Array and how see the array?
11,725
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;
}
Author by
Johnny
Updated on June 04, 2022Comments
-
Johnny almost 2 years
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" } }