I have an int[3][3] array and it contains only 0 or 1 values, if the value is 1 I want to add the coordinates of this value in the ArrayList as int[2] array, but I don\'t know w
If you are putting Points into an ArrayList, I suggest you create a Point object that takes coordinates. Please refer to the code below. Sorry for the lengthy reply.
import java.util.ArrayList;
import java.util.Arrays;
public class SomeClass {
static class Point {
int[] coordinates;
public Point(int x, int y) {
this.coordinates = new int[2];
this.coordinates[0] = x;
this.coordinates[1] = y;
}
public Point() {
this(0,0);
}
public Point(int[] coordinates) {
this.coordinates = coordinates;
}
}
public static void main(String[] args) {
SomeClass myClass = new SomeClass();
Point a = new Point();
Point b = new Point(5,5);
Point c = new Point(new int[]{3,4});
ArrayList arr = new ArrayList();
// adding
arr.add(a);
arr.add(b);
arr.add(c);
// retrieve one object
int index = 0;
Point retrieved = arr.get(index);
System.out.println("Retrieved coordinate: " + Arrays.toString(retrieved.coordinates));
retrieved.coordinates[0] = 15;
retrieved.coordinates[1] = 51;
System.out.println("After change, retrieved coordinate: " + Arrays.toString(retrieved.coordinates));
System.out.println("After change, accessing arraylist index: " + Arrays.toString(arr.get(index).coordinates));
// we received a pointer to the array
// changed values are automatically reflected in the ArrayList
}
}
These are the values you will get...
Retrieved coordinate: [0, 0]
After change, retrieved coordinate: [15, 51]
After change, accessing arraylist index: [15, 51]