logo

Java.util.ArrayList.add() metodas Java

Žemiau pateikiami add() metodai ArrayList Java kalba:

    loginis add(Object o) : Šis metodas prideda nurodytą elementą prie šio sąrašo pabaigos.
     Parameters: object o: The element to be appended to this list. Exception: NA>








    užblokuoti numeriai
    // 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);> >}> >}> }>



    >

    >

    Išvestis:

     Number = 15 Number = 20 Number = 25>
    void add(int index, Object element) : šis metodas įterpia nurodytą elementą E į nurodytą vietą šiame sąraše. Jis perkelia šiuo metu esantį elementą (jei yra) ir visus vėlesnius elementus į dešinę (prie jų pridės vieną elementą). indeksai).
     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);> >}> >}> }>

    >

    >

    Išvestis:

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