Java에서 ArrayList의 하위 목록 가져 오기
Java에서 ArrayList의 일부를 얻으려면 subList() />List subList(int fromIndex, int toIndex)
fromIndex이 포함되고 toIndex 제외( [fromIndex, toIndex[ )입니다. 이 메서드는 list 유형의 객체를 반환하므로 하위 목록을 다른 ArrayList에 저장하려면 List에서 이 ArrayList를 만들어야 합니다. new ArrayList(input.subList(fromIndex, toIndex)) 입니다. 반면에 결과 하위 목록을 List에 저장하면 example.
import java.util.ArrayList;런타임:
import java.util.List;
public class ArrayListSublist {
public static void main(String[] args) {
// ArrayList를 만듭니다< 문자열>
ArrayList< 문자열> aList = 새로운 ArrayList< 문자열> ();
//ArrayList에 문자열 추가
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
System.out.println("배열 목록");
for(문자열 e:aList)
System.out.println(e);
목록 목록 = aList.subList(1, 4);
System.out.println("ArrayList ");
for(int i=0; i< list.size(); i++)
System.out.println(list.get(i));
}
}
ArrayList
1
2
3
4
5
ArrayList
2
3
4
Remaque:
subList() 메서드는 IndexOutOfBoundsException 지정된 인덱스가 0보다 작거나 size.
IllegalArgumentException fromIndex가 toIndex보다 큰 경우, 즉 fromIndex > toIndex입니다.
subList() 메서드는 IndexOutOfBoundsException 지정된 인덱스가 0보다 작거나 size.
IllegalArgumentException fromIndex가 toIndex보다 큰 경우, 즉 fromIndex > toIndex입니다.