Java Arrays                                            

In Java, an array is used to store a list of values of the same data type. They can also be referred to as a collection of variables of the same type referred to by one common name.

 

Example

Suppose you have a favourite colour for example “blue” and would like to store it, in such a case a single variable is perfect for this. However, in case you have more than one favourite color you’ll need to declare a separate variable for each color. As shown below.

 

 

Now this is not convenient, especially when storing many values, say 1,2,3 or even 5,7 and more. In this case, we usually store values in arrays or lists.

Difference between an array and a list

In Java, there is a difference between arrays and lists but in many programming languages, they are pretty much the same. The main difference is that; whereas arrays have fixed length, lists are resizable and more dynamic. In most cases, in Java, you will encounter lists probably more than arrays.

 

How to create an array

To create an array we need to define the type of variable with square brackets followed by the name of the array, in this article we will name it “colours”, followed by an equals sign and curly braces in the end.

We have declared a variable named “colors” that will hold a Java array of favourite colours that are of data type “String”. To populate the array with values, place the name of colors {“blue”, “green”, “orange”} in a comma-separated list within the curly braces.

 

 

Once we create the array we can print out the whole array or a selected item from the array using their respective index numbers. This can be accomplished using the print function and square bracket after the name of the array System.out.println(colors[]).

Basically, we have to place the index of the item that we intend to print out within the square brackets. In the array, we have just created, “blue” is going to be the 0th element while “green” and “orange” are 1st and 2nd respectively. This will print out the name of the colors as shown below.

 

 

We can also print out all the elements of the array using a for loop. We are going to loop through the array until we reach the maximum number of items that are in the “colors” array. In order to obtain the number of elements that are present in the array we are going to leverage the String method “colors.length”. And now we can actually add more elements into the array for example we can add “yellow” and all of them are going to be printed out.

 

 

Arrays can also be modified just like in the case of variables. For instance, we can decide to change the second color from “orange” to “pink”.We can do this by leveraging the index number of the element we want to modify as shown below.

 

 

This is not the easiest way to add new elements to the array since we have to rewrite the for loop in order to print out the newly added elements. Alternatively, we can also rewrite the element in the already existing array but that is not possible either. In such a case an ArrayList is a more convenient way of handling multiple elements in Java.

 

Learn Java Arrays and Arraylists online

ArrayLists

Creating an ArrayList

ArrayLists unlike arrays allow us to add and remove elements freely while in the case of arrays we have to create an entirely new array. We can easily create an ArrayList using the following statement ArrayList<String> colorList = new ArrayList<>( ); where: “colorList” is the name of the ArrayList and “ArrayList” is a class that we have to import from the “java.util” package. Then we are going to create a new ArrayList, before adding a couple of elements into it. 

We are going to use the “add” method to add elements into the new Java ArrayList we have just created as follows: colorList.add(“green”), colorList.add(“orange”), colorList.add(“yellow”). We now have all the elements in the Java ArrayList that we just created courtesy of the code below.

 

 

We can perform various operations on a Java ArrayList including printing out elements, removing and adding new elements. Printing out elements requires that we pass the name of the Java ArrayList into this function System.out.println(colorList).If we want to remove an element from the Java ArrayList we will use the “remove” function and we can either remove it by value or by index/number. 

Besides removing an element we can also add new ones as well as override the already existing ones by using the “colorList.set” method and declaring the index of the element we want to override.

 

 

There are going to be a lot of things that we can actually do once we have created a Java ArrayList. We can add multiple elements to a specific index, check if something is already in the list for example you can check if it contains “blue”.

We can also determine the number of elements that are in a Java ArrayList by using the colorList.size() method and even sort out the colors in alphabetical order by using the method “Collections.sort(colorList)”.

These are the most important things that one can do with the Java ArrayList, and this is how we can store multiple items in Java. Finally, we can also print out the elements in the list one by one using a for loop and “i”  as an index, using the size of the Java ArrayList as the limit of the number of times we want to loop through the list. As shown in the code below.

 

 

If you’d like to try out the sample codes on your own, you can do it by an online Java compiler for free. Here are a few examples for online Java compilers:

 

In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.