언어/JAVA
java : ArrayList : subList, reverse
하이후에호
2020. 5. 13. 22:13
반응형
자바에서 ArrayList를 나눌 때는 subList라는 것을 사용한다.
ArrayList<Integer> next = new ArrayList<Integer>(list[y][x].subList(index, length));
Collections.reverse(next);
list[y][x] = new ArrayList<Integer>(list[y][x].subList(0, index));
하지만 위처럼 next리스트에 subList를 넣을때는 new ArrayList를 해서 동적할당해준다. 이유는
list.subList의 반환형은 List이기 때문에 할당 되지 않는다.
마찬가지로 Collections.reverse를 사용해서 ArrayList를 역배열로 선언할 수 있다.
subList(x,y) 는 x번째 인덱스부터 y번째까지를 반환한다. 즉.. x , x+1 ,x+2 .... y-1 index를 반환한다.
반응형