List.subList(from, to) 메소드가 당연히 from <= list <=to 인줄 알았는데,

알고보니. from <= list < to 였다. toIndex 자리의 



api에 보니. toIndex 는 exclusive 라.... 그 위치는 제외된다. 


예제)

public class Test {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();

list.add("a");

list.add("b");

list.add("c");

list.add("d");

System.out.println(list.subList(0, 2));

}

}

  

나의 예상은 abc 였건만.. 결과는 2개였다. 


결과

[a, b]



<API>

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)


substring

public String substring(int beginIndex,
               int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"
 
Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.


Posted by '김용환'
,