Java Array to List: An Overview and Code Examples

java array to listIn Java, an Array is a primitive holder of objects of the same type, while a List is one of the abstract interfaces of the Collection interface.

A List offers more control over the operations of that group of Objects. To manipulate an Array, you have to create methods to manipulate its contents manually. With a List, you just need to call its methods. A List automatically arranges objects in a definite sequence, while an Array maintains the order you pass to it originally.

For an introduction to Objects and Interfaces, visit Udemy.com

Create and Change Array to List Examples

Let us say you have a group of country names that you need to manipulate. The simplest way would be to create an Array to contain them.

Program 1: Create simple Arrays

import java.util.Arrays;

class createSimpleArrays{

public static void main(String[] args){

//Create an Array of String type Objects, method 1

String countryNames1 = new String[]{

“Canada”,

“Ukrain”,

“Nigeria”,

“Japan”,

“Brazil”};

 

//Create an Array of String type Objects, method 2

String countryNames2 = new String[5];

countryNames2[0] = “Canada”;

countryNames2[1] = “Ukrain”;

countryNames2[2] = “Nigeria”;

countryNames2[3] = “Japan”;

countryNames2[4] = “Brazil”;

//Test if it returns the data you provided

System.out.println(“Country 1 is: ” + countryNames2[0]);

System.out.println(“Country 1 is: ” + countryNames2[1]);

System.out.println(“Country 1 is: ” + countryNames2[2]);

System.out.println(“Country 1 is: ” + countryNames2[3]);

System.out.println(“Country 1 is: ” + countryNames2[4]);

}

}

In the above code, you have a String Array (called countryNames) that contains five elements. Note that you have to define the type of Objects (in this case String) that you insert into the Array; otherwise, your program will not run.

Which prints:

Country 1 is: Canada

Country 2 is: Ukrain

Country 3 is: Nigeria

Country 4 is: Japan

Country 5 is: Brazil

Remember, an Array does not maintain any logical order, for example, an alphabetical order. Therefore, if your country names Array contained all the countries in the world, you would have to maintain a separate record to keep track of which position you placed a country’s name. That is why you need another facility to do all the hard the work.

Now that you have a group of country names already, let us put it into a List.

Program 2: Create simple Lists

import java.util.Arrays;

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

 

class createSimpleLists{

public static void main(String[] args){

//Create an Array of String type Objects

String countryNames = new String[]{

“Canada”,

“Ukrain”,

“Nigeria”,

“Japan”,

“Brazil”};

 

//Create a List of String type Objects, method 1

List<String> countryNamesList1 = Arrays.asList(countryNames);

//Create a List of String type Objects, method 2

List<String> countryNamesList2 = new ArrayList<String>();

Collections.addAll(countryNamesList2, countryNames);

 

//Test if the list contains the data we provided

for(int i=0; i < countryNamesList2.size(); i++){

System.out.println(“Country names in List 2 …”);

System.out.println(countryNamesList2.get(i));

}

 

//Alternatively, if you want to have control over which countries you

//place into the List, create a List of String type Objects with method 3

List<String> countryNamesList3 = new ArrayList<String>();

countryNamesList3.add(“Canada”);

countryNamesList3.add(“Nigeria”);

countryNamesList3.add(“Japan”);

countryNamesList3.add(“Brazil”);

 

//Test if the list contains the data we provided

for(int j=0; j < countryNamesList3.size(); j++){

System.out.println(“Country names in List 3 …”);

System.out.println(countryNamesList3.get(j));

}

}

}

Learn more about the for loop and other loop types at Udemy.com

Method 1 creates a List that prohibits you from modifying the content. That is, you cannot later change the name Ukrain to Ukraine. However, mistakes happen when naming countries, so let’s create a List that is more forgiving such as method 2.

As method 3 shows, if we are not sure if we spell a country’s name as Ukrainor Ukraine, we leave it for a later time.

On running the createSimpleLists program, we get the following output,

Country names in List 2 …

Canada

Ukrain

Nigeria

Japan

Brazil

 

Country names in List 3 …

Canada

Nigeria

Japan

Brazil

What Lists Offer Compared to Arrays

Now that you’ve turned an Array into a List, you have more capabilities of manipulating the data. For example, we combine elements from several unique Lists, search the List, change the order of elements, and iterate over the List.

Just as method 3, from Program 2 illustrated, a List has the facility to add (and in turn remove) elements from its queue. You have to call the add method to append an element at the end of the List’s queue, while calling the remove method would drop the specified element in its first instance.

When you compare how you can do this in an Array’s scenario, you realize just how much work a List saves you.

Code Snippet 1: Add/Remove operations in a List and Array scenario

//countryNames is list of String Objects that represent country names

countryNames.add(“South Africa”);

countryNames.remove(“Ukrain”);

/*In an Array’s case, the operations above have to include creating a new

Array that contains the elements of the previous Array, plus/minus

the required element*/

 

Good Java coding practice requires you to create as few Objects as possible during the runtime of a program. This means, you should declare a variable, for example countryNames, in Code Snippet 1, only when necessary. In a List’s case, when you want to reuse, you simply remove all the resident elements and insert others, using the clear method. However, in an Arrays case, you have to re-declare it using the new elements.

Visit Udemy.com for courses on how you can manipulate Arrays

In a case where you have two lists with different elements, but would like to have a single List that contains the elements from both the different Lists, you may use the addAll method to append all the elements from one List to the end of the specified List. However, to achieve this with the elements of two Arrays, you use a multi-step process that first creates an Array big enough to hold the elements of the two smaller Arrays, before reading each element from the smaller Arrays and copying them into the bigger Array.

Perhaps the most noticeable operation is when you intend to change an element in a List, compared to when you do the same in an Array. In a List’s case, it is as simple as the set method. However, in an Array’s case this requires you to create a new Array with all the required elements, while leaving out the undesired one.