Home » Core Java » util » Arrays » java.util.Arrays Example

About Armando Flores

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.

java.util.Arrays Example

In this example, we will show the range of functionality provided by the java.util.Arrays class.

The Arrays 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 Java 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.

 

1. Example of java.util.Arrays methods

JavaUtilArraysExample.java

package com.javacodegeeks.examples;

import java.util.Arrays;
import java.util.List;

public class JavaUtilArraysExample {
	public static void main(String[] args) {
		// Base array for the example. It contains 9 elements.
		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));

		// Returns a fixed-size list backed by the specified array.
		List integerList = Arrays.asList(integerArray);

		// Returned list length
		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 };
		
		// Sorts the specified array into ascending numerical order.
		System.out.printf("Unsorted baseArray: %s\n", Arrays.toString(baseArray));
		
		Arrays.sort(baseArray);
		
		System.out.printf("Sorted baseArray: %s\n", Arrays.toString(baseArray));
		
		// Searches the specified array of ints for the specified value
		// using the binary search algorithm.
		int idx = Arrays.binarySearch(baseArray, 21);
		System.out.printf("Value \"21\" found at index: %d\n\n", idx);
		
		// Copies the specified array, truncating or padding with zeros (if necessary)
		// so the copy has the specified length.
		int[] copyOfArray = Arrays.copyOf(baseArray, 11);
		
		System.out.printf("baseArray size: %d\n", baseArray.length);
		System.out.printf("baseArray elements: %s\n", Arrays.toString(baseArray));
		System.out.printf("copyOfArray size: %d\n", copyOfArray.length);
		System.out.printf("copyOfArray elements: %s\n\n", Arrays.toString(copyOfArray));
		
		// Copies the specified range of the specified array into a new array.		
		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));
		
		// Assigns the specified int value to each element of the specified array of ints.
		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));
	}
}

Let’s explain the methods used in the above example.

If we run the above code, we will get the following results:

integerArray size: 9
integerArray elements: [2, 4, 3, 7, 21, 9, 98, 76, 74]
integerList size: 9
integerList 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: 5

baseArray size: 9
baseArray elements: [2, 3, 4, 7, 9, 21, 74, 76, 98]
copyOfArray size: 11
copyOfArray 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 Arrays class

JavaUtilArraysMoreMethodsExample.java

package com.javacodegeeks.examples;

import java.util.Arrays;

public class JavaUtilArraysMoreMethodsExample {
	public static void main(String[] args) {
		// Base array for the example. It contains 9 elements.
		int[] baseArray = { 2, 4, 3, 7, 21, 9, 98, 76, 74 };
		
		// Sorts the specified range of the array into ascending order.
		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));
		
		// Assigns the specified int value to each element of the
		// specified range of the specified array of ints.
		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));
	}
}

Let’s explain the methods used in the above example.

If we run the above code, we will get the following results:

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]

 

3. Download the source code

You can download the source code of this example from here: JavaUtilArraysClass.zip

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of