Array Lists In Java

This lesson will cover Java Arraylists with examples in order to meet the learners’ needs.

Java arraylists

Java ArrayLists

As far as Java ArrayList is concerned, this class belongs to the Java.util package and is a resizable array.

As compared to ArrayList ,a built-in array in Java cannot be modified by adding or removing elements (you must create a new one if you want to insert or delete elements). On the other hand, an ArrayList allows you to add and remove elements at any time.

There is also a slight difference in syntax.

You will need to create an ArrayList object called firms, which will store strings:

import java.util.ArrayList; // This imports the ArrayList class
ArrayList<String> firms = new ArrayList<String>(); // Here we have created an object for the ArrayList

An ArrayList object named ‘numbers’ needs to be created, which will store integers:

import java.util.ArrayList; // The ArrayList class gets imported here
ArrayList<String> numbers = new ArrayList<String>(); // Here we have created an object for the ArrayList
Learn about Java packages if you’re unfamiliar with the concept.


Java ArrayList Add()

A number of useful methods are available in the ArrayList class. If you want to add elements to an ArrayList in Java, you can use the add() method:

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList firms = new ArrayList(); firms.add(" Python"); firms.add("Java"); firms.add("Ruby"); firms.add("JavaScript"); firms.add("C/C++"); System.out.println(firms); } }

Java ArrayList Add() examples

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList numbers= new ArrayList();numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5);System.out.println(numbers); } }

Java ArrayList Get() an Item

ArrayList elements are accessed using the get() method and the index number:

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList firms = new ArrayList(); firms.add(" Python"); firms.add("Ruby"); firms.add("Java"); firms.add("JavaScript"); firms.add("C/C++"); // To access Java from the above given ArrayList we can specify the index number of the Java (2) inside the get method System.out.println("We are learning : "+firms.get(2)); } }

Similarly, in order to obtain the value of 5 from the numbers ArrayList given above follow the example below:

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList numbers= new ArrayList();numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5);// Here we specify the index number 4 in the .get() method in order to get the value of 5 System.out.println("Value: "+numbers.get(4)); } }

 

Keep in mind: The first element of an array is indexed by 0: [0]. Accordingly, [1] represents the second element, etc.


Java ArrayList Set() an Item

Java Arraylists use the set() method to modify elements, referring to their index numbers as follows:

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList alphabets=new ArrayList();alphabets.add('A'); alphabets.add('B'); alphabets.add('C'); alphabets.add('D'); alphabets.add('E'); alphabets.set(2,'F'); // Modifies the value at index 2 to FSystem.out.println(alphabets);} }

Another Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList billionaires =new ArrayList();billionaires.add("Bernard Arnault"); billionaires.add("Elon Musk"); billionaires.add("Gautam Adani"); billionaires.add("Bill Gates"); billionaires.add("Jeff Bezos");billionaires.set(2,"Warren Buffett");System.out.println(billionaires);} }

 


Java ArrayList Remove() an Item

In Java Arraylist, refer to the index number when removing an element using the remove() method:

Example: 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList numbers= new ArrayList();numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5);// If we use to remove the value at index 3 we simply write the index number of the element inside the remove methodint removed=numbers.remove(3); // The deleted element from the ArrayList is stored in a variable named removeSystem.out.println("Number removed: "+removed); System.out.println(numbers); } }

Example: 2 

import java.util.ArrayList;public class Main { public static void main(String[] args) {ArrayList laptop_brands= new ArrayList();laptop_brands.add("Dell"); laptop_brands.add("Hp"); laptop_brands.add("Samsung"); laptop_brands.add("Asus"); laptop_brands.add("Acer");System.out.println("Old ArrayList: "+laptop_brands); String removed=laptop_brands.remove(2); // We have use a variable named remove in order to keep the record of the deleted element from the ArrayList System.out.println("\nName removed: "+removed); System.out.println("New ArrayList: "+laptop_brands); } }

You can also utilize the clear() method to remove all elements from the ArrayList:

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList alphabets=new ArrayList();alphabets.add('A'); alphabets.add('B'); alphabets.add('C'); alphabets.add('D'); alphabets.add('E');System.out.println("Before Clearing the Elements from the Arraylist: "+alphabets);alphabets.clear();System.out.println("After Clearing the Elements from the Arraylist: "+alphabets);} }

Example 2: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList cars=new ArrayList();cars.add("Tesla"); cars.add("BMW"); cars.add("Mercedes"); cars.add("Range Rover"); cars.add("Audi");System.out.println("Before Clearing the elements from the Arraylist: "+cars);cars.clear();System.out.println("After Clearing all the elements from the Arraylist: "+cars);} }

ArrayList Size

Using the size method, you can determine the number of elements in an ArrayList:

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList nba_Teams=new ArrayList();nba_Teams.add("Boston Celtics"); nba_Teams.add("Chicago Bulls"); nba_Teams.add("Atlanta Hawks "); nba_Teams.add("Brooklyn Nets"); nba_Teams.add("Cleveland Cavaliers");System.out.println("The size of ArrayList is: "+nba_Teams.size());} }

Another Approach

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList airplanes=new ArrayList();airplanes.add("Airbus"); airplanes.add("Jets"); airplanes.add("Concorde"); airplanes.add("Boeing");System.out.println("The size of ArrayList Airplanes is: "+airplanes.size());} }

Loop Through an ArrayList

You can specify how many times a loop should run using the size() method when looping through an arraylist using a for loop:

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList nfl_Teams=new ArrayList();nfl_Teams.add("Chicago Bears"); nfl_Teams.add("Cleveland Browns"); nfl_Teams.add("Dallas Cowboys"); nfl_Teams.add("Denver Broncos"); nfl_Teams.add("Detroit Lions"); nfl_Teams.add("Houston Texans"); nfl_Teams.add("Green Bay Packers"); nfl_Teams.add("Jacksonville Jaguars");for(int mrx=0;mrx<nfl_Teams.size();mrx++){ System.out.println(nfl_Teams.get(mrx)); } } }

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList agricultural_countries=new ArrayList();agricultural_countries.add("China"); agricultural_countries.add("United States"); agricultural_countries.add("Brazil."); agricultural_countries.add("India"); agricultural_countries.add("Russia"); agricultural_countries.add("France"); agricultural_countries.add("Mexico");for(int ample=0;ample<agricultural_countries.size();ample++){ System.out.println(agricultural_countries.get(ample)); } } }

The for-each loop can also be used to loop through an ArrayList:

Example:1 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList odd_Numbers =new ArrayList();odd_Numbers.add(1); odd_Numbers.add(3); odd_Numbers.add(5); odd_Numbers.add(7); odd_Numbers.add(9); odd_Numbers.add(11); odd_Numbers.add(13);for (int ample: odd_Numbers){ System.out.println(ample); } } }

 

Example:2 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList vowels =new ArrayList();vowels.add('A'); vowels.add('E'); vowels.add('I'); vowels.add('O'); vowels.add('U');for (char ample: vowels){ System.out.println(ample); } } }

Other Types

ArrayLists contain objects as elements. Presented above is an example of creating elements (objects) of type “String” and “int”.

Keep in mind that Strings in Java are objects (not primitive types).

If you wish to use another type, such as int, an equivalent wrapper class must be specified: Integer.

You can also use Boolean when representing boolean values, character when representing char values, and double when representing double values:

The following code will build an arraylist for storing numbers (add elements of type Double):

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList double_values =new ArrayList();double_values.add(23.432d); double_values.add(73.23412d); double_values.add(543.3252d); double_values.add(75.5432d); double_values.add(1412.522d);for (double mrx: double_values){ System.out.println(mrx); } } }

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList boolean_values =new ArrayList();boolean_values.add(true); boolean_values.add(false);for(boolean ample: boolean_values){ System.out.println(ample); } } }

The following example shows an ArrayList containing heights of different individuals:

Example: 

import java.util.ArrayList; public class Main { public static void main(String[] args) {ArrayList heights_in_feet =new ArrayList();heights_in_feet.add(5.4f); heights_in_feet.add(5.6f); heights_in_feet.add(5.2f); heights_in_feet.add(5.7f); heights_in_feet.add(6.1f);for (float ample: heights_in_feet){ System.out.println(ample); } } }

Java ArrayList Sort()

Java’s java.util package has other useful classes, one of which is the Collections class, which includes the sort() method for sorting lists alphabetically or numerically.

An ArrayList of characters can be sorted in the following way:

Example: 

import java.util.ArrayList; import java.util.Collections; // Collections class is imported here public class Main { public static void main(String[] args) {ArrayList consonants =new ArrayList();consonants.add('F'); consonants.add('Z'); consonants.add('P'); consonants.add('K'); consonants.add('L');Collections.sort(consonants); for(char ample: consonants){ System.out.println(ample); } } }

An ArrayList of Integers is sorted as follows:

Example: 

import java.util.ArrayList; import java.util.Collections; // Collections class is imported herepublic class Main { public static void main(String[] args) {ArrayList odd_Nums =new ArrayList();odd_Nums.add(7); odd_Nums.add(9); odd_Nums.add(5); odd_Nums.add(3); odd_Nums.add(1);Collections.sort(odd_Nums); // By using class named 'Collections' , we here sort the ArrayList in Ascending Orderfor(int mrx: odd_Nums){ System.out.println(mrx); } } }

Here we have sorted as ArrayList of Strings :

Example: 

import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) {ArrayList nfl_Teams=new ArrayList();nfl_Teams.add("Chicago Bears"); nfl_Teams.add("Cleveland Browns"); nfl_Teams.add("Dallas Cowboys"); nfl_Teams.add("Denver Broncos"); nfl_Teams.add("Detroit Lions"); nfl_Teams.add("Houston Texans"); nfl_Teams.add("Green Bay Packers"); nfl_Teams.add("Jacksonville Jaguars");System.out.println("\nBefore Sorting ArrayList: \n"); for(String mrx: nfl_Teams){ System.out.println(mrx); }Collections.sort(nfl_Teams);System.out.println("\nAfter Sorting ArrayList: \n"); for(String mrx: nfl_Teams){ System.out.println(mrx); } } }

Similar sorting can also be done on an ArrayList of floats, as shown below:

Example: 

import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) {ArrayList float_values =new ArrayList();float_values.add(5.44f); float_values.add(1.41f); float_values.add(22.52f); float_values.add(2.54f); float_values.add(13.51f);System.out.println("\nBefore Sorting ArrayList: \n"); for(float mrx: float_values){ System.out.println(mrx); }Collections.sort(float_values);System.out.println("\nAfter Sorting ArrayList: \n"); for(float ample: float_values){ System.out.println(ample); } } }

Now that you know what Java ArrayLists are and how they can be used, you will be able to use them more effectively.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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