Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Arrays in Java: A Reference Guide

$
0
0

1. Introduction

In this tutorial, we’ll deep dive into a core concept in the Java language – arrays.

We’ll first see what’s an array, then how to use them; overall, we’ll cover how to:

  • Get started with arrays
  • Read and write arrays elements
  • Loop over an array
  • Transform arrays into other objects like List or Streams
  • Sort, search and combine arrays

2. What’s an Array?

First things first, we need to define what’s an array? According to the Java documentation, an array is an object containing a fixed number of values of the same type. The elements of an array are indexed, which means we can access them with numbers (called indices).

We can consider an array as a numbered list of cells, each cell being a variable holding a value. In Java, the numbering starts at 0.

There are primitive type arrays and object type arrays. This means we can use arrays of int, float, boolean, … But also arrays of String, Object and custom types as well.

3. Setting up an Array

Now that arrays are well-defined, let’s dive into their usages.

We’ll cover a lot of topics teaching us how to use arrays. We’ll learn some basics like how to declare and initialize an array, but we’ll also cover more advanced subjects like sorting and searching arrays.

Let’s go first with declaration and initialization.

3.1. Declaration

We’ll begin with the declaration. There are two ways to declare an array in Java:

int[] anArray;

or:

int anOtherArray[];

The former is more widely used than the latter.

3.2. Initialization

Now that it’s time to see how to initialize arrays. Again there are multiple ways to initialize an array. We’ll see the main ones here, but this article covers arrays initialization in detail.

Let’s begin with a simple way:

int[] anArray = new int[10];

By using this method, we initialized an array of ten int elements. Note that we need to specify the size of the array.

When using this method, we initialize each element to its default value, here 0. When initializing an array of Object, elements are null by default.

We’ll now see another way giving us the possibility to set values to the array directly when creating it:

int[] anArray = new int[] {1, 2, 3, 4, 5};

Here, we initialized a five element array containing numbers 1 to 5. When using this method we don’t need to specify the length of the array, it’s the number of elements then declared between the braces.

4. Accessing Elements

Let’s now see how to access the elements of an array. We can achieve this by requiring an array cell position.

For example, this little code snippet will print 10 to the console:

anArray[0] = 10;
System.out.println(anArray[0]);

Note how we are using indices to access the array cells. The number between the brackets is the specific position of the array we want to access.

When accessing a cell, if the passed index is negative or goes beyond the last cell, Java will throw an ArrayIndexOutOfBoundException.

We should be careful then not to use a negative index, or an index greater than or equal to the array size.

5. Iterating Over an Array

Accessing elements one by one can be useful, but we might want to iterate through an array. Let’s see how we can achieve this.

The first way is to use the for loop:

int[] anArray = new int[] {1, 2, 3, 4, 5};
for (int i = 0; i < anArray.length; i++) {
    System.out.println(anArray[i]);
}

This should print numbers 1 to 5 to the console. As we can see we made use of the length property. This is a public property giving us the size of the array.

Of course, it’s possible to use other loop mechanisms such as while or do while. But, as for Java collections, it’s possible to loop over arrays using the foreach loop:

int[] anArray = new int[] {1, 2, 3, 4, 5};
for (int element : anArray) {
    System.out.println(element);
}

This example is equivalent to the previous one, but we got rid of the indices boilerplate code. The foreach loop is an option when:

  • we don’t need to modify the array (putting another value in an element won’t modify the element in the array)
  • we don’t need the indices to do something else

6. Varargs

We’ve already covered the basics when it comes to the creation and manipulation of arrays. Now, we’ll dive into more advanced topics, beginning with varargs. As a reminder, varargs are used to pass an arbitrary number of arguments to a method:

void varargsMethod(String... varargs) {}

This method could take from 0 to an arbitrary number of String arguments. An article covering varargs can be found here.

What we have to know here is that inside the method body, a varargs parameter turns into an array. But, we can also pass an array directly as the argument. Let’s see how by reusing the example method declared above:

String[] anArray = new String[] {"Milk", "Tomato", "Chips"};
varargsMethod(anArray);

Will behave the same as:

varargsMethod("Milk", "Tomato", "Chips");

7. Transforming an Array into a List

Arrays are great, but sometimes it can be handier to deal with List instead. We’ll see here how to transform an array into a List.

We’ll first do it the naïve way, by creating an empty list and iterating over the array to add its elements to the list:

int[] anArray = new int[] {1, 2, 3, 4, 5};

List<Integer> aList = new ArrayList<>();
for (int element : anArray) {
    aList.add(element);
}

But there is another way, a little bit more succinct:

Integer[] anArray = new Integer[] {1, 2, 3, 4, 5};
List<Integer> aList = Arrays.asList(anArray);

The static method Arrays.asList takes a varargs argument and creates a list with the passed values. Unfortunately, this method comes with some drawbacks:

  • It’s not possible to use an array of primitive types
  • We can’t add or remove elements from the created list, as it’ll throw an UnsupportedOperationException

8. From an Array to a Stream

We can now transform arrays into lists, but since Java 8 we have access to the Stream API and we might want to turn our arrays into Stream. Java provides us with the Arrays.stream method for that:

String[] anArray = new String[] {"Milk", "Tomato", "Chips"};
Stream<String> aStream = Arrays.stream(anArray);

When passing an Object array to the method it will return a Stream of the matching type (e.g. Stream<Integer> for an array of Integer). When passing a primitive one it will return the corresponding primitive Stream.

It’s also possible to create the stream only on a subset of the array:

Stream<String> anotherStream = Arrays.stream(anArray, 1, 3);

This will create a Stream<String> with only “Tomato” and “Chips” Strings (the first index being inclusive while the second one is exclusive).

9. Sorting Arrays

Let’s now see how to sort an array, that is rearranging its elements in a certain order. The Arrays class provides us with the sort method. A bit like the stream method, sort has a lot of overloadings.

There are overloadings to sort:

  • Primitive type arrays: which are sorted in ascending order
  • Object arrays (those Object must implement the Comparable interface): which are sorted according to the natural order (relying on the compareTo method from Comparable)
  • Generic arrays: which are sorted according to a given Comparator

In addition, it’s possible to sort only a specific portion of an array (passing start and end indices to the method).

The algorithms behind the sort method are quick sort and merge sort for primitive and other arrays, respectively.

Let’s see how this all work through some examples:

int[] anArray = new int[] {5, 2, 1, 4, 8};
Arrays.sort(anArray); // anArray is now {1, 2, 4, 5, 8}

Integer[] anotherArray = new Integer[] {5, 2, 1, 4, 8};
Arrays.sort(anotherArray); // anotherArray is now {1, 2, 4, 5, 8}

String[] yetAnotherArray = new String[] {"A", "E", "Z", "B", "C"};
Arrays.sort(yetAnotherArray, 1, 3, 
  Comparator.comparing(String::toString).reversed()); // yetAnotherArray is now {"A", "Z", "E", "B", "C"}

Searching an array is pretty simple, we can loop over the array and search our element among the array elements:

int[] anArray = new int[] {5, 2, 1, 4, 8};
for (int i = 0; i < anArray.length; i++) {
    if (anArray[i] == 4) {
        System.out.println("Found at index " + i);
        break;
    }
}

Here we searched for number 4 and found it at index 3.

If we have a sorted array though, we can use another solution: the binary search. The principle of binary search is explained in this article.

Fortunately, Java provides us with the Arrays.binarySearch method. We have to give it an array and an element to search.

In case of a generic array, we also have to give it the Comparator that was used to sort the array in the first place. There is again the possibility to call the method on a subset of the array.

Let’s see an example of the binary search method usage:

int[] anArray = new int[] {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(anArray, 4);
System.out.println("Found at index " + index);

As we stored number 4 in the fourth cell, this will return index 3 as the result. Note that we used an already sorted array.

11. Concatenating Arrays

Finally, let’s see how to concatenate two arrays. The idea is to create an array which length is the sum of the two arrays to concatenate. After that we have to add the elements of the first one and then the elements of the second one:

int[] anArray = new int[] {5, 2, 1, 4, 8};
int[] anotherArray = new int[] {10, 4, 9, 11, 2};

int[] resultArray = new int[anArray.length + anotherArray.length];
for (int i = 0; i < resultArray.length; i++) {
    resultArray[i] = (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length]);
}

As we can see, when the index is still lesser than the first array length we add elements from that array. Then we add elements from the second one. We can make use of the Arrays.setAll method to avoid writing a loop:

int[] anArray = new int[] {5, 2, 1, 4, 8};
int[] anotherArray = new int[] {10, 4, 9, 11, 2};

int[] resultArray = new int[anArray.length + anotherArray.length];
Arrays.setAll(resultArray, i -> (i < anArray.length ? anArray[i] : anotherArray[i - anArray.length]));

This method will set all array element according to the given function. This function associates an index with a result.

Here is a third option to merge to arrays: System.arraycopy. This method takes a source array, a source position, a destination array, a destination position and an int defining the number of elements to copy:

System.arraycopy(anArray, 0, resultArray, 0, anArray.length);
System.arraycopy(anotherArray, 0, resultArray, anArray.length, anotherArray.length);

As we can see, we copy the first array, then the second (after the last element of the first one).

12. Conclusion

In this detailed article we’ve covered basic and some advanced usages of arrays in Java.

We saw that Java offers a lot of methods to deal with arrays through the Arrays utility class. There also are utility classes to manipulate arrays in libraries such as Apache Commons or Guava.

The full code for this article can be found on our GitHub.


Viewing all articles
Browse latest Browse all 4535

Trending Articles