Java의 Java.util.ArrayList.add() 메소드

다음은 add() 메소드입니다. 배열목록 자바에서:

    boolean add(Object o) : 이 메소드는 지정된 요소를 이 목록의 끝에 추가합니다.
     Parameters: object o: The element to be appended to this list. Exception: NA 




    // Java code to illustrate add(Object o)> import> java.io.*;> import> java.util.ArrayList;> > public> class> ArrayListDemo {> public> static> void> main(String[] args)> > {> > > // create an empty array list with an initial capacity> > ArrayList arrlist => new> ArrayList(> 5> );> > > // use add() method to add elements in the list> > arrlist.add(> 15> );> > arrlist.add(> 20> );> > arrlist.add(> 25> );> > > // prints all the elements available in list> > for> (Integer number : arrlist) {> > System.out.println(> 'Number = '> + number);> > }> > }> }>

    산출:

     Number = 15 Number = 20 Number = 25 
    void add(int index, Object element) : 이 메소드는 지정된 요소 E를 이 목록의 지정된 위치에 삽입합니다. 현재 해당 위치에 있는 요소(있는 경우)와 후속 요소를 오른쪽으로 이동합니다(해당 요소에 하나를 추가합니다). 지수).
     Parameters: index : The index at which the specified element is to be inserted. element :  The element to be inserted. Exception: Throws IndexOutOfBoundsException if the specified index is out of range (index size()). 




    // Java code to illustrate> // void add(int index, Object element)> import> java.io.*;> import> java.util.ArrayList;> > public> class> ArrayListDemo {> public> static> void> main(String[] args)> > {> > > // create an empty array list with an initial capacity> > ArrayList arrlist => new> ArrayList(> 5> );> > > // use add() method to add elements in the list> > arrlist.add(> 10> );> > arrlist.add(> 22> );> > arrlist.add(> 30> );> > arrlist.add(> 40> );> > > // adding element 35 at fourth position> > arrlist.add(> 3> ,> 35> );> > > // let us print all the elements available in list> > for> (Integer number : arrlist) {> > System.out.println(> 'Number = '> + number);> > }> > }> }>

    산출:

     Number = 10 Number = 22 Number = 30 Number = 35 Number = 40