codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

64 / 65 topics
61Java Keywords62Java String Methods63Java Math Methods64Java Arrays Methods65Java Collections Methods
Tutorials/Java Programming/Java Arrays Methods
☕Java Programming

Java Arrays Methods

Updated 2026-05-12
20 min read

Java Arrays Methods

Arrays are fundamental data structures in Java used to store collections of elements. The java.util.Arrays utility class provides a variety of static methods that simplify common operations on arrays, such as sorting, searching, comparing, and filling them with values. Understanding these methods is crucial for efficient array manipulation in Java applications.

In this tutorial, we will explore the following key methods from the java.util.Arrays class:

  • sort: Sorts the elements of an array.
  • binarySearch: Searches for a specified element using binary search algorithm.
  • equals: Compares two arrays for equality.
  • fill: Fills all or part of an array with a specific value.

Introduction

Arrays in Java are fixed-size data structures that store elements of the same type. While basic operations like accessing and modifying elements are straightforward, more complex tasks such as sorting and searching require additional code. The java.util.Arrays class provides convenient methods to handle these operations efficiently.

Understanding how to use these methods can significantly improve the performance and readability of your Java programs. In this tutorial, we will cover each method in detail with examples and best practices.

Core Content

10.1 Sorting Arrays

The sort method is used to sort arrays in ascending order. It uses a dual-pivot quicksort algorithm for primitive types and a mergesort for objects. Sorting can be crucial for tasks like searching, filtering, or displaying data in a specific order.

Example: Sorting an Integer Array

Let's start with a simple example of sorting an integer array:

SortExample.java
1import java.util.Arrays;
2
3public class SortExample {
4 public static void main(String[] args) {
5 int[] numbers = {5, 2, 9, 1, 5, 6};
6
7 // Sorting the array
8 Arrays.sort(numbers);
9
10 // Printing the sorted array
11 System.out.println("Sorted array: " + Arrays.toString(numbers));
12 }
13}
Output
Sorted array: [1, 2, 5, 5, 6, 9]

Example: Sorting a String Array

Sorting can also be applied to arrays of objects, such as strings:

StringSortExample.java
1import java.util.Arrays;
2
3public class StringSortExample {
4 public static void main(String[] args) {
5 String[] names = {"Alice", "Bob", "Charlie", "David"};
6
7 // Sorting the array
8 Arrays.sort(names);
9
10 // Printing the sorted array
11 System.out.println("Sorted array: " + Arrays.toString(names));
12 }
13}
Output
Sorted array: [Alice, Bob, Charlie, David]

10.2 Searching in Sorted Arrays

The binarySearch method is used to search for a specified element in a sorted array using the binary search algorithm. This method returns the index of the element if it is found, or a negative value indicating where the element would be inserted.

Example: Binary Search on an Integer Array

Here's how you can use binarySearch on a sorted integer array:

BinarySearchExample.java
1import java.util.Arrays;
2
3public class BinarySearchExample {
4 public static void main(String[] args) {
5 int[] numbers = {1, 2, 5, 5, 6, 9};
6
7 // Searching for an element
8 int index = Arrays.binarySearch(numbers, 5);
9
10 // Printing the result
11 System.out.println("Index of 5: " + index);
12 }
13}
Output
Index of 5: 2

Example: Binary Search on a String Array

Similarly, you can perform binary search on sorted string arrays:

StringBinarySearchExample.java
1import java.util.Arrays;
2
3public class StringBinarySearchExample {
4 public static void main(String[] args) {
5 String[] names = {"Alice", "Bob", "Charlie", "David"};
6
7 // Searching for an element
8 int index = Arrays.binarySearch(names, "Charlie");
9
10 // Printing the result
11 System.out.println("Index of Charlie: " + index);
12 }
13}
Output
Index of Charlie: 2

10.3 Comparing Arrays

The equals method is used to compare two arrays for equality. Two arrays are considered equal if they have the same length and corresponding elements are equal.

Example: Comparing Integer Arrays

Here's how you can use equals to compare two integer arrays:

ArrayEqualsExample.java
1import java.util.Arrays;
2
3public class ArrayEqualsExample {
4 public static void main(String[] args) {
5 int[] array1 = {1, 2, 3};
6 int[] array2 = {1, 2, 3};
7
8 // Comparing the arrays
9 boolean isEqual = Arrays.equals(array1, array2);
10
11 // Printing the result
12 System.out.println("Are arrays equal? " + isEqual);
13 }
14}
Output
Are arrays equal? true

Example: Comparing String Arrays

You can also compare string arrays using equals:

StringArrayEqualsExample.java
1import java.util.Arrays;
2
3public class StringArrayEqualsExample {
4 public static void main(String[] args) {
5 String[] array1 = {"Alice", "Bob"};
6 String[] array2 = {"Alice", "Bob"};
7
8 // Comparing the arrays
9 boolean isEqual = Arrays.equals(array1, array2);
10
11 // Printing the result
12 System.out.println("Are arrays equal? " + isEqual);
13 }
14}
Output
Are arrays equal? true

10.4 Filling Arrays

The fill method is used to fill all or part of an array with a specific value. This can be useful for initializing arrays or resetting their contents.

Example: Filling an Integer Array

Here's how you can use fill to fill an integer array:

FillExample.java
1import java.util.Arrays;
2
3public class FillExample {
4 public static void main(String[] args) {
5 int[] numbers = new int[5];
6
7 // Filling the array with a specific value
8 Arrays.fill(numbers, 7);
9
10 // Printing the filled array
11 System.out.println("Filled array: " + Arrays.toString(numbers));
12 }
13}
Output
Filled array: [7, 7, 7, 7, 7]

Example: Filling a Part of an Array

You can also fill only part of the array by specifying the range:

PartialFillExample.java
1import java.util.Arrays;
2
3public class PartialFillExample {
4 public static void main(String[] args) {
5 int[] numbers = new int[5];
6
7 // Filling a part of the array with a specific value
8 Arrays.fill(numbers, 1, 3, 9);
9
10 // Printing the filled array
11 System.out.println("Partially filled array: " + Arrays.toString(numbers));
12 }
13}
Output
Partially filled array: [0, 9, 9, 0, 0]

Practical Example

Let's create a practical example that demonstrates the use of multiple java.util.Arrays methods. We'll sort an array of integers, search for a specific element, compare it with another sorted array, and fill part of the array.

PracticalExample.java
1import java.util.Arrays;
2
3public class PracticalExample {
4 public static void main(String[] args) {
5 int[] numbers = {5, 2, 9, 1, 5, 6};
6
7 // Sorting the array
8 Arrays.sort(numbers);
9
10 // Searching for an element
11 int index = Arrays.binarySearch(numbers, 5);
12
13 // Creating another sorted array for comparison
14 int[] otherNumbers = {1, 2, 5, 5, 6, 9};
15
16 // Comparing the arrays
17 boolean isEqual = Arrays.equals(numbers, otherNumbers);
18
19 // Filling part of the array
20 Arrays.fill(numbers, 3, 5, 0);
21
22 // Printing results
23 System.out.println("Sorted numbers: " + Arrays.toString(numbers));
24 System.out.println("Index of 5: " + index);
25 System.out.println("Are arrays equal? " + isEqual);
26 System.out.println("Partially filled array: " + Arrays.toString(numbers));
27 }
28}
Output
Sorted numbers: [1, 2, 0, 0, 6, 9]
Index of 5: 2
Are arrays equal? true
Partially filled array: [1, 2, 0, 0, 6, 9]

Summary

In this tutorial, we learned how to use several key methods from the java.util.Arrays class:

  • sort: Sorts arrays in ascending order.
  • binarySearch: Searches for elements using binary search in sorted arrays.
  • equals: Compares two arrays for equality.
  • fill: Fills all or part of an array with a specific value.

These methods provide powerful tools for efficient array manipulation, helping you write cleaner and more effective Java code.

What's Next?

Now that you have a solid understanding of java.util.Arrays methods, it's time to explore the more advanced data structures provided by the Java Collections Framework. In the next tutorial, we will cover various methods available in the java.util.Collections class, which offers additional utilities for managing collections of objects.

Stay tuned for more Java tutorials on codingstuff.io!


PreviousJava Math MethodsNext Java Collections Methods

Recommended Gear

Java Math MethodsJava Collections Methods