티스토리 뷰

2차원 배열 생성하면서 객체 리스트를 넣고 싶을 때

ArrayList<객체> map [][] = new ArrayList[N][N];

for(int r = 0; r< N; r++){
	for(int c=0; c<N; c++){
    	map[r][c] = new ArrayList<객체>(); // 초기화 필수
	}
}

int r1 = Integer.parseInt(br.readLine());
int c1 = Integer.parseInt(br.readLine());

map[r1][c1].add(new 객체(객체 값)); // add로 해서 해당 위치에 리스트에 객체 넣어주기~!

 

이런 식으로 2차원 배열에 객체를 넣고 싶을 때 저런 형태로 사용하면 된다!

삼성 상어 잡으러 가다가 만난 벽......ㅎㅔ.....

 

응용버전

Queue<객체>  map[][] = new LinkedList[N][N];

List, Map, Set 등 앞에 2차원 배열 요소로  Collection 삽입 가능 

댓글