반응형
add(Object o)
Appends the specified element to the end of this list
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
Appends the specified element to the end of this list
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
add(Object o)
Appends the specified element to the end of this list.
Appends the specified element to the end of this list.
먼저 영문을 볼때 해석에 주의 해야겠다.
위의 add Method는 class java.util.ArrayList의 Method이다.
위의 두 Method의 차이점에 대해서 생각해 보자.
같은 Method이지만 arguments에 따라서 Overloading 된다.
중요한 점은 add(int index, Object element) 인 add Method는 만일 ArrayList에 add(Object o) Method에 의해 값이 들어가 있지 않을 경우
(index < 0 || index > size()조건에서 size() 의 값이 0이 들어가 있으므로
같은 Method이지만 arguments에 따라서 Overloading 된다.
중요한 점은 add(int index, Object element) 인 add Method는 만일 ArrayList에 add(Object o) Method에 의해 값이 들어가 있지 않을 경우
(index < 0 || index > size()조건에서 size() 의 값이 0이 들어가 있으므로
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:371)
at foo.ArrTest.main(ArrTest.java:33)
Exception in thread "main"
at java.util.ArrayList.add(ArrayList.java:371)
at foo.ArrTest.main(ArrTest.java:33)
Exception in thread "main"
위와 같은 Error를 뱉어낸다.
-----------------------------------------------------------------------------------
/**
*
*/
package foo;
import java.util.ArrayList;
/**
* @author jhkim
*
*/
public class ArrTest {
/**
*
*/
public ArrTest() {
// TODO 자동 생성된 생성자 스텁
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 자동 생성된 메소드 스텁
ArrayList arrlist = new ArrayList();
//System.out.print(arrlist.size());
//arrlist.add("3");
//arrlist.add("4");
//arrlist.add("5");
//arrlist.add("6");
arrlist.add(1,"a");
arrlist.add(2,"b");
arrlist.add(3,"c");
String c = (String)arrlist.get(3);
System.out.println(c);
}
}
-------------------------------------------------------------------------
위의 예제에서는 Error 위에서 언급한 Error가 난다.
하지만 주석을 제거하여 arrlist에 값을 4개 넣어주면 먼저 3,4,5,6 이라는 값이 들어가고
그 다음 array[1] 에는 a가 추가되고 4,5,6은 각각 array[2],array[3],array[4]로 순차적으로 미뤄지고 size는 3이었던게 4로 늘어난다.
그래서 최종적으로
arrlist의 값은 이러하다.
[0] "3"
[1] "a"
[2] "b"
[3] "c"
[4] "4"
[5] "5"
[6] "6"
-----------------------------------------------------------------------------------
/**
*
*/
package foo;
import java.util.ArrayList;
/**
* @author jhkim
*
*/
public class ArrTest {
/**
*
*/
public ArrTest() {
// TODO 자동 생성된 생성자 스텁
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 자동 생성된 메소드 스텁
ArrayList arrlist = new ArrayList();
//System.out.print(arrlist.size());
//arrlist.add("3");
//arrlist.add("4");
//arrlist.add("5");
//arrlist.add("6");
arrlist.add(1,"a");
arrlist.add(2,"b");
arrlist.add(3,"c");
String c = (String)arrlist.get(3);
System.out.println(c);
}
}
-------------------------------------------------------------------------
위의 예제에서는 Error 위에서 언급한 Error가 난다.
하지만 주석을 제거하여 arrlist에 값을 4개 넣어주면 먼저 3,4,5,6 이라는 값이 들어가고
그 다음 array[1] 에는 a가 추가되고 4,5,6은 각각 array[2],array[3],array[4]로 순차적으로 미뤄지고 size는 3이었던게 4로 늘어난다.
그래서 최종적으로
arrlist의 값은 이러하다.
[0] "3"
[1] "a"
[2] "b"
[3] "c"
[4] "4"
[5] "5"
[6] "6"
반응형