ArrayLists in Java And Method Implementation with Examples

       ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may not know until run time precisely how large an array you need. To handle this situation, the Collections Framework defines ArrayList.

       In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array can be shrunk.

       Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. 

       Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

1.

add(E e): Appends the specified element to the end of this list.
				
					 List<Integer> myList=new ArrayList<>();
        myList.add(1);
        myList.add(2);
        myList.add(3);
        myList.add(4);
        myList.add(5);
        System.out.println(myList);
				
			

2.

add(int index, E element) Inserts the specified element at the specified position in this list.
				
					List<Integer> myList=new ArrayList<>();
       //Adding elements at a Particular index
        myList.add(0,1);
        myList.add(1,2);
        myList.add(2,3);
        myList.add(3,5);
        myList.add(4,4);
        System.out.println(myList);
				
			

3.

addAll(Collection c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator.
				
					List<Integer> myList=new ArrayList<>();
       //Adding Elements as a group
        myList.addAll(Arrays.asList(1,2,3,4,5));
        System.out.println(myList);
				
			

4.

clear() Removes all of the elements from this list.
				
					List<Integer> myList=new ArrayList<>();
       //flushing out the data from arraylist
        myList.addAll(Arrays.asList(1,2,3,4,5));
        myList.clear();
        System.out.println(myList);

				
			

5.

contains(Object o) Returns true if this list contains the specified element.
				
					
List<Integer> myList=new ArrayList<>();
       //Check whether the Arraylist contains the Given data
        myList.addAll(Arrays.asList(1,2,3,4,5));
        Boolean Avail=myList.contains(3);
        System.out.println(Avail);
		
				
			

6.

get(int index) Returns the element at the specified position in this list.
				
					List<Integer> myList=new ArrayList<>();
       //Gets the item at position
        myList.addAll(Arrays.asList(1,2,3,44,5));
        System.out.println(myList.get(4));
				
			

7.

iterator() Returns an iterator over the elements in this list in proper sequence.
				
							
List<Integer> myList = new ArrayList<>();
        //Check whether the Arraylist contains the Given data
        myList.addAll(Arrays.asList(1, 2, 3, 44, 5));
        Iterator<Integer> iter = myList.iterator();

        // Displaying the values
        // after iterating through the list
        System.out.println("\nThe iterator values"
                + " of list are: ");
        while (iter.hasNext()) {
            System.out.print(iter.next() + " ");

        }
				
			

8.

remove(int index) Removes the element at the specified position in this list.
				
					List<Integer> myList = new ArrayList<>();
        //remove at position
        myList.addAll(Arrays.asList(1, 2, 3, 44, 5));
        myList.remove(4);
        System.out.println(myList);
		
				
			

9.

size() Returns the number of elements in this list.
				
					List<Integer> myList = new ArrayList<>();
        //Check the size of array list
        myList.addAll(Arrays.asList(1, 2, 3, 44, 5));
        System.out.println(myList.size());
		
				
			

Thats It for the Topic! Thanks You Guys For Visiting And Make Sure To Comment!

Source: Arraylist in java

Do Visit : Best IDE For Python – Code-Tech Community (codetech.tech)

Happy Coding Mates 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Shopping Cart