Element in ArrayList Java
Learn to get an element from an ArrayList using its index position. We will be using ArrayList.get() method to get the object at the specified index of the arraylist. Show
1. ArrayList get() MethodArrayList.get(int index) method returns the element at the specified position 'index' in the list. 1.1. Syntaxpublic Object get( int index );1.2. Method Parameterindex – index of the element to return. A valid index is always be between 0 (inclusive) to the size of ArrayList (exclusive). For example, if ArrayList holds 10 objects then a valid argument index will be between 0 to 9 (both inclusive). 1.3. Return ValueThe get() method returns the reference of the object present at the specified index. 1.4. IndexOutOfBoundsExceptionAn invalid index argument will cause IndexOutOfBoundsException error. Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:12)2. ArrayList get() ExampleJava program for how to get an object from ArrayList by its index location. In this example, we want to get the object stored at index locations 0 and 1. import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main(String[] args) { ArrayListProgram output. alex brianHappy Learning !! Let us know if you liked the post. That’s the only way we can improve.
The ArrayList class of the Java collections framework provides the functionality of resizable-arrays. It implements the List interface. Java ArrayList Vs ArrayIn Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it's hard to change it. To handle this issue, we can use the ArrayList class. It allows us to create resizable arrays. Unlike arrays, arraylists can automatically adjust its capacity when we add or remove elements from it. Hence, arraylists are also known as dynamic arrays. Creating an ArrayListBefore using ArrayList, we need to import the java.util.ArrayList package first. Here is how we can create arraylists in Java: ArrayListHere, Type indicates the type of an arraylist. For example, // create Integer type arraylist ArrayListIn the above program, we have used Integer not int. It is because we cannot use primitive types while creating an arraylist. Instead, we have to use the corresponding wrapper classes. Here, Integer is the corresponding wrapper class of int. To learn more, visit the Java wrapper class. Example: Create ArrayList in Javaimport java.util.ArrayList; class Main { public static void main(String[] args){ // create ArrayList ArrayList Output ArrayList: [Java, Python, Swift]In the above example, we have created an ArrayList named languages. Here, we have used the add() method to add elements to the arraylist. We will learn more about the add() method later in this tutorial. Basic Operations on ArrayListThe ArrayList class provides various methods to perform different operations on arraylists. We will look at some commonly used arraylist operations in this tutorial:
1. Add Elements to an ArrayListTo add a single element to the arraylist, we use the add() method of the ArrayList class. For example, import java.util.ArrayList; class Main { public static void main(String[] args){ // create ArrayList ArrayList// add() method without the index parameter languages.add("Java"); languages.add("C"); languages.add("Python"); System.out.println("ArrayList: " + languages); } }Output In the above example, we have created an ArrayList named languages. Here, we have used the add() method to add elements to languages. To learn more, visit the Java ArrayList add(). We can also pass an index number as an additional parameter to the add() method to add an element at the specified position. For example, // add JavaScript at index 1 languages.add(1, "JavaScript"); // add C++ at index 3 languages.add(3, "C++");We can also add all elements of a collection (set, map) to an arraylist using the addAll() method. For example, import java.util.ArrayList; import java.util.HashSet; class Main { public static void main(String[] args) { // create a set HashSet// add all elements of set to arraylist alphabets.addAll(vowels); System.out.println("ArrayList: " + alphabets); } } // Output: ArrayList: [a, e, i]To learn more, visit Java ArrayList addAll(). 2. Access ArrayList ElementsTo access an element from the arraylist, we use the get() method of the ArrayList class. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList// get the element from the arraylist String str = animals.get(1); System.out.print("Element at index 1: " + str); } }Output ArrayList: [Cat, Dog, Cow] Element at index 1: DogIn the above example, we have used the get() method with parameter 1. Here, the method returns the element at index 1. To learn more, visit the Java ArrayList get(). We can also access elements of the ArrayList using the iterator() method. To learn more, visit Java ArrayList iterator(). 3. Change ArrayList ElementsTo change elements of the arraylist, we use the set() method of the ArrayList class. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList// change the element of the array list languages.set(2, "JavaScript"); System.out.println("Modified ArrayList: " + languages); } }Output ArrayList: [Java, Kotlin, C++] Modified ArrayList: [Java, Kotlin, JavaScript]In the above example, we have created an ArrayList named languages. Notice the line, language.set(2, "JavaScript");Here, the set() method changes the element at index 2 to JavaScript. To learn more, visit the Java ArrayList set(). To remove an element from the arraylist, we can use the remove() method of the ArrayList class. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList// remove element from index 2 String str = animals.remove(2); System.out.println("Updated ArrayList: " + animals); System.out.println("Removed Element: " + str); } }Output ArrayList: [Dog, Cat, Horse] Updated ArrayList: [Dog, Cat] Removed Element: HorseHere, the remove() method takes the index number as the parameter. And, removes the element specified by the index number. To learn more, visit the Java ArrayList remove(). We can also remove all the elements from the arraylist at once. To learn more, visit
Methods of ArrayList ClassIn the previous section, we have learned about the add(), get(), set(), and remove() method of the ArrayList class. Besides those basic methods, here are some more ArrayList methods that are commonly used.
If you want to learn about all the different methods of arraylist, visit Java ArrayList methods. Iterate through an ArrayListWe can use the Java for-each loop to loop through each element of the arraylist. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { // creating an array list ArrayListfor (String language : animals) { System.out.print(language); System.out.print(", "); } } }Output ArrayList: [Cow, Cat, Dog] Accessing individual elements: Cow, Cat, Dog,Some of the major differences between ArrayList and LinkedList in Java are:
We can convert the ArrayList into an array using the toArray() method. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList// convert ArrayList into an array languages.toArray(arr); System.out.print("Array: "); // access elements of the array for (String item : arr) { System.out.print(item + ", "); } } }Output ArrayList: [Java, Python, C++] Array: Java, Python, C++,In the above example, the toArray() method converts the languages arraylist to an array and stores it in arr. To learn more, visit Java ArrayList toArray(). We use the asList() method of the Arrays class. To use asList(), we must import the java.util.Arrays package first. For example, import java.util.ArrayList; import java.util.Arrays; class Main { public static void main(String[] args) { // create an array of String type String[] arr = { "Java", "Python", "C++" }; System.out.print("Array: "); // print array for (String str : arr) { System.out.print(str); System.out.print(" "); } // create an ArrayList from an array ArrayList Output In the above program, we first created an array arr of the String type. Notice the expression, Arrays.asList(arr)Here, the asList() method converts the array into an arraylist. We use the Arrays.asList() method to create and initialize an arraylist in a single line. For example, import java.util.ArrayList; import java.util.Arrays; class Main { public static void main(String[] args) { // create and initialize ArrayList ArrayList We use the toString() method of the ArrayList class to convert an arraylist into a string. For example, import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList// convert ArrayList into a String String str = languages.toString(); System.out.println("String: " + str); } }Output ArrayList: [Java, Python, Kotlin] String: [Java, Python, Kotlin]Here, the toString() method converts the whole arraylist into a single string. To learn more, visit Java ArrayList toString(). We can also create an arraylist using the List interface. It's because the ArrayList class implements the List interface. Let's see an example, import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { // create arraylist using List List |