Java Array – java.util.Arrays Example (with Video) (original) (raw)

In this example, we will explain the array definition and we will show the range of functionality provided by the Java arrays class: java.util.Arrays. This class of the java.util package contains several static methods that you can use to compare, sort, and search in arrays. In addition, you can use this class to assign a value to one or more elements of an array. This class is a member of the Collections Framework.

An array of integers (int[]) will be used as our base array in the following example to illustrate most of the methods provided by the java.util.Arrays class. But before diving into the practical examples, let us understand the different types that are available in the Java programming language.

You can also check this tutorial in the following video:

Java Array Example How to use Arrays in Java – Video

But let’s examine the array definition, by using the following examples.

1. Example of Java Arrays methods

Now let us start with an example to understand the basic declaration and different methods in Arrays. But before digging deep let us look at different methods we will be using in this example.

Java Array

1.1 Arrays.toString() method

This method returns the String representation of the array enclosed in the square brackets ([]). Adjacent elements are separated by the comma character (i.e. a comma followed by a space). Let us understand this with a simple example.

Snippet

12 Integer[] integerArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 };System.out.printf("integerArray elements: %s\n", Arrays.toString(integerArray));

1.2 Arrays.asList() method

This method returns a list backed by a given array. In other words, both the list and array refer to the same location. Let us understand this with a simple example.

Snippet

1 List integerList = Arrays.asList(integerArray);

1.3 Arrays.sort() method

This method sorts the specified array into ascending numerical order. Let us understand this with a simple example.

Snippet

1.4 Arrays.binarySearch() method

This method returns an integer value for the index of the specified key in the specified array. Return a negative number if the key is not found and for this method to work properly, the array must be sorted. Let us understand this with a simple example.

Snippet

1 int idx = Arrays.binarySearch(baseArray, 21);

1.5 Arrays.copyOf() method

This method copies the specified array, truncates or pads with zeros (if necessary) so the copy has the specified length. Let us understand this with a simple example.

Snippet

1 int[] copyOfArray = Arrays.copyOf(baseArray, 11);

1.6 Arrays.copyOfRange() method

This method copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. Let us understand this with a simple example.

Snippet

1 int[] copyOfRangeArray = Arrays.copyOfRange(baseArray, 5, 8);

1.7 Arrays.fill() method

This method fills all elements of the specified array with the specified value. Let us understand this with a simple example.

Snippet

1234 int[] fillArray = new int[5]; System.out.printf("fillArray (before): %s\n", Arrays.toString(fillArray));Arrays.fill(fillArray, 1);System.out.printf("fillArray (after): %s", Arrays.toString(fillArray));

1.8 Complete Example

Let us consider the below example where we will illustrate all the methods explained above.

JavaUtilArraysExample.java

0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647 package com.javacodegeeks.examples;import java.util.Arrays;import java.util.List;public class JavaUtilArraysExample { public static void main(String[] args) { Integer[] integerArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 }; System.out.printf("integerArray size: %d\n", integerArray.length); System.out.printf("integerArray elements: %s\n", Arrays.toString(integerArray)); List integerList = Arrays.asList(integerArray); System.out.printf("integerList size: %d\n", integerList.size()); System.out.printf("integerList elements: "); for (Integer i : integerList) { System.out.printf("%d ", i); } System.out.printf("\n\n"); int[] baseArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 }; System.out.printf("Unsorted baseArray: %s\n", Arrays.toString(baseArray)); Arrays.sort(baseArray); System.out.printf("Sorted baseArray: %s\n", Arrays.toString(baseArray)); int idx = Arrays.binarySearch(baseArray, 21); System.out.printf("Value \"21\" found at index: %d\n\n", idx); System.out.printf("baseArray size: %d\n", baseArray.length); System.out.printf("baseArray elements: %s\n", Arrays.toString(baseArray)); int[] copyOfArray = Arrays.copyOf(baseArray, 11); System.out.printf("copyOfArray size: %d\n", copyOfArray.length); System.out.printf("copyOfArray elements: %s\n\n", Arrays.toString(copyOfArray)); System.out.printf("baseArray: %s\n", Arrays.toString(baseArray)); int[] copyOfRangeArray = Arrays.copyOfRange(baseArray, 5, 8); System.out.printf("copyOfRangeArray: %s\n\n", Arrays.toString(copyOfRangeArray)); int[] fillArray = new int[5]; System.out.printf("fillArray (before): %s\n", Arrays.toString(fillArray)); Arrays.fill(fillArray, 1); System.out.printf("fillArray (after): %s", Arrays.toString(fillArray)); }}

If everything goes well, we will get the following results in the console.

010203040506070809101112131415 integerArray size: 9integerArray elements: [2, 4, 3, 7, 21, 9, 98, 76, 74]integerList size: 9integerList elements: 2 4 3 7 21 9 98 76 74 Unsorted baseArray: [2, 4, 3, 7, 21, 9, 98, 76, 74]Sorted baseArray: [2, 3, 4, 7, 9, 21, 74, 76, 98]Value "21" found at index: 5baseArray size: 9baseArray elements: [2, 3, 4, 7, 9, 21, 74, 76, 98]copyOfArray size: 11copyOfArray elements: [2, 3, 4, 7, 9, 21, 74, 76, 98, 0, 0]baseArray: [2, 3, 4, 7, 9, 21, 74, 76, 98]copyOfRangeArray: [21, 74, 76]fillArray (before): [0, 0, 0, 0, 0]fillArray (after): [1, 1, 1, 1, 1]

2. Some more methods of the Java Array class

Java Arrays add some interesting method arguments to the existing sort() and fill() method i.e.

Let us understand these modifications with an example.

JavaUtilArraysMoreMethodsExample.java

01020304050607080910111213141516171819202122232425 package com.javacodegeeks.examples;import java.util.Arrays;public class JavaUtilArraysMoreMethodsExample { public static void main(String[] args) { int[] baseArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 }; System.out.printf("Unsorted baseArray: %s\n", Arrays.toString(baseArray)); Arrays.sort(baseArray, 1, 6); System.out.printf("Sorted baseArray: %s\n\n", Arrays.toString(baseArray)); int[] fillArray = new int[10]; System.out.printf("fillArray (before): %s\n", Arrays.toString(fillArray)); Arrays.fill(fillArray, 1, 7, 3); System.out.printf("fillArray (after): %s", Arrays.toString(fillArray)); }}

If everything goes well, we will get the following results in the console.

1234 Unsorted baseArray: [2, 4, 3, 7, 21, 9, 98, 76, 74]Sorted baseArray: [2, 3, 4, 7, 9, 21, 98, 76, 74]fillArray (before): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]fillArray (after): [0, 3, 3, 3, 3, 3, 3, 0, 0, 0]

4. Download the Source Code

That was an article explaining the array definition in Java through examples.

Last updated on Jan. 25th, 2022

Photo of Armando Flores

Armando graduated from from Electronics Engineer in the The Public University Of Puebla (BUAP). He also has a Masters degree in Computer Sciences from CINVESTAV. He has been using the Java language for Web Development for over a decade. He has been involved in a large number of projects focused on "ad-hoc" Web Application based on Java EE and Spring Framework.