ArrayList in Java
ArrayList in Java, ArrayList is a Class which is use dynamic Arrays to Store Elements.
Things to be Take Note about ArrayList:
- In an ArrayList Class,Element will be shift forward, when an element is being removed from the ArrayList. So the Processing time will be slow
- You can access any Arraylist Elements according to its Array Index Number.
- ArrayList Class is non Synchronized
- ArrayList Class maintain its insertion Order
- Arraylist Class allows duplicate Elements.
In this article. I will create an ArrayList Example Code and step through , the basic method used for an ArrayList Class.
class arrayLists
- First I have created a class called ” arrayLists”
- And then you will need to import java.util.ArrayList;import java.util.*; both of this Library.
- Create a new arrayLists object called “animals” .” ArrayList<String> animals = new ArrayList<String>();”
- Use the new instance ” animals ” you have just created to add elements into the ArrayList “animals.add()”.
- Check the Size of the Array List. int size = animals.size();
- Print out the individual Object of the arraylists animals.get(1)
- Use For Loop to loop through the Element in the Arraylist
- Use For Each Loop “for (String animal : animals) “ to Loop through the Element in the ArrayList.
- Use “animals.remove(size – 1); “ to remove the last element ” Hamster ” from the array list.
- Use the For each Loop to print out the elements inside the Arraylists
- Use the “Iterator” to print out all elements inside the ArrayLists.
import java.util.ArrayList; import java.util.*; public class arrayLists { public static void main(String[] args) { // Declaration of a new Array List ArrayList<String> animals = new ArrayList<String>(); // Adding to the List animals.add("dog"); animals.add("Cat"); animals.add("hamster"); // Get the Size int size = animals.size(); System.out.println("The size of the list is: " + size); // Get the Object in the List System.out.println("The item on index 1 is: " + animals.get(1)); // Iteration using the ForEach Loop System.out.println("Iteraton example"); for (int i = 0; i < size; i++) { System.out.println("Item on index " + i + " is: " + animals.get(i)); } System.out.println("\nNext for loop example"); for (String animal : animals) { System.out.println("The item is: " + animals); } // Remove the Last Item Hamster animals.remove(size - 1); System.out.println("\nNext for loop example after removing"); for (String animal : animals) { System.out.println("The item is: " + animals); } // Iteration using iterator Iterator itr= animals.iterator(); while(itr.hasNext()){ System.out.println("Print Out using Iterator"); System.out.println(itr.next()); } } }
Add Instance Object into Array List
- First I have created a class called petstore;
- Then I create a Constructor for the Class
- Then i create another Class I called it “accessPet”< to ACCESS the petstore class
- Create new Instance of “petstores” named Snoopy and Garfields
- Create new ArrayList for petstores called “cartoonAnimal”
- Add ” Snoopy and Garfield “ into the newly created Arraylist called ” CartoonAnimal”
- Iterate Through and Print out all member in the Arraylist
Class petstores
package petstore; public class petstores { String petname; String petType; int petAge; // Create a Constructor public petstores(String petname , String petType ,int petAge){ this.petname =petname; this.petType =petType; this.petAge =petAge; } }
Class accessPet
package petstore; import java.util.*; public class accessPet { public static void main(String[] args) { // TODO Auto-generated method stub petstores snoopy = new petstores("snoopy","doggy" , 2); petstores garfields = new petstores("garfields", "kitty",3); // Create a new ArrayList called " cartoonAnimal" ArrayList<petstores> cartoonAnimal =new ArrayList<petstores>(); // Add "snoopy " instance into the Array List " cartoonAnimal" // Add "Garfiels " instance into the Array List " cartoonAnimal" cartoonAnimal.add(snoopy); cartoonAnimal.add(garfields); Iterator itr = cartoonAnimal.iterator(); //traversing elements of ArrayList object while(itr.hasNext()){ petstores ct=(petstores)itr.next(); System.out.println(ct.petname+" "+ct.petType+" "+ct.petAge); } } }
Check out method of Java arraylist here
Check out Exception Handling in Java here
ArrayList in Java Method
No | Method | Description |
---|---|---|
1 | void trimToSize() | trim the size of the ArrayList according to the input Parameter. |
2 | int indexOf(Object o) | Return index in this list of the first occurrence of the specific element, or return -1 if the List does not contain this element. |
3 | boolean addAll(int index, Collection c) | Insert elements in the specific collection into this list, starting at the specified position. |
4 | boolean add(Object o) | Append the specified element to the end of a list. |
5 | Object[] toArray(Object[] a) | Return array containing elements in this list in the correct sequence. |
6 | int lastIndexOf(Object o) | Return the index in this list of the last occurrence of theelement, or -1 if the list does not contain this element. |
7 | void clear() | Remove all of the elements from the list. |
8 | boolean addAll(Collection c) | append all of the elements in the specific collection to the end of list, to returned by the specified collection’s iterator. |
9 | void add(int index, Object element) | Insert the specific element at the specified position index in the list. |
Leave a Reply