Вопрос

I have a 2D array of objects that will be changing creating new instances of different objects. Is it possible to have one of my objects get its coordinates with a constructor and put them into an ArrayList of Points? The idea is for these types of objects to be able to "see" each other or know the positions of the others.

public static void main(String[] args){
    Object [][] objArray = new Object[10][10];

    for (int i=0;i<10;i++){
        for (int j=0;j<10;j++){
            objArray[i][j]= new Class1();
        }

    }

}

public class Class1{
ArrayList<Point> coordArray = new ArrayList<Point>();

Class1(){
    int x = 0, y = 0;

    //x = objArray. get cord x?
    //y = objArray. get cord y?
    coordArray.add(new Point(x,y));
}

}

Это было полезно?

Решение

Is this what you're trying to do?

public static void main(String[] args){
    Object [][] objArray = new Object[10][10];

    for (int i=0;i<10;i++){
        for (int j=0;j<10;j++){
            objArray[i][j]= new Class1(i, j);
        }
    }

}

public class Class1{
    ArrayList<Point> coordArray = new ArrayList<Point>();

    Class1(int x, int y){
        coordArray.add(new Point(x,y));
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top