문제

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