🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The java.util.Arrays class contains various methods for manipulating arrays (such as sorting and searching). This guide covers the most important APIs provided by the Arrays class, along with examples and outputs.
Table of Contents
Arrays.toString()Arrays.sort()Arrays.binarySearch()Arrays.equals()Arrays.copyOf()Arrays.copyOfRange()Arrays.fill()Arrays.asList()Arrays.stream()Arrays.deepToString()Arrays.deepEquals()Arrays.mismatch()- Conclusion
1. Arrays.toString()
Converts the array to a string representation.
Example:
import java.util.Arrays;
public class ArraysToStringExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
String arrayString = Arrays.toString(numbers);
System.out.println("Array: " + arrayString);
}
}
Output:
Array: [1, 2, 3, 4, 5]
2. Arrays.sort()
Sorts the array in ascending order.
Example:
import java.util.Arrays;
public class ArraysSortExample {
public static void main(String[] args) {
int[] numbers = {5, 3, 1, 4, 2};
Arrays.sort(numbers);
System.out.println("Sorted Array: " + Arrays.toString(numbers));
}
}
Output:
Sorted Array: [1, 2, 3, 4, 5]
3. Arrays.binarySearch()
Searches the array for a specific value using the binary search algorithm. The array must be sorted before calling this method.
Example:
import java.util.Arrays;
public class ArraysBinarySearchExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int index = Arrays.binarySearch(numbers, 3);
System.out.println("Index of 3: " + index);
}
}
Output:
Index of 3: 2
4. Arrays.equals()
Checks if two arrays are equal.
Example:
import java.util.Arrays;
public class ArraysEqualsExample {
public static void main(String[] args) {
int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
int[] array3 = {4, 5, 6};
boolean isEqual = Arrays.equals(array1, array2);
System.out.println("Array1 equals Array2: " + isEqual);
isEqual = Arrays.equals(array1, array3);
System.out.println("Array1 equals Array3: " + isEqual);
}
}
Output:
Array1 equals Array2: true
Array1 equals Array3: false
5. Arrays.copyOf()
Copies the specified array, truncating or padding with zeros if necessary, to fit the specified length.
Example:
import java.util.Arrays;
public class ArraysCopyOfExample {
public static void main(String[] args) {
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, 5);
System.out.println("Original Array: " + Arrays.toString(original));
System.out.println("Copied Array: " + Arrays.toString(copy));
}
}
Output:
Original Array: [1, 2, 3]
Copied Array: [1, 2, 3, 0, 0]
6. Arrays.copyOfRange()
Copies the specified range of the specified array into a new array.
Example:
import java.util.Arrays;
public class ArraysCopyOfRangeExample {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOfRange(original, 1, 4);
System.out.println("Original Array: " + Arrays.toString(original));
System.out.println("Copied Range: " + Arrays.toString(copy));
}
}
Output:
Original Array: [1, 2, 3, 4, 5]
Copied Range: [2, 3, 4]
7. Arrays.fill()
Fills the specified array with the specified value.
Example:
import java.util.Arrays;
public class ArraysFillExample {
public static void main(String[] args) {
int[] numbers = new int[5];
Arrays.fill(numbers, 10);
System.out.println("Filled Array: " + Arrays.toString(numbers));
}
}
Output:
Filled Array: [10, 10, 10, 10, 10]
8. Arrays.asList()
Converts an array to a List.
Example:
import java.util.Arrays;
import java.util.List;
public class ArraysAsListExample {
public static void main(String[] args) {
String[] array = {"A", "B", "C"};
List<String> list = Arrays.asList(array);
System.out.println("Array: " + Arrays.toString(array));
System.out.println("List: " + list);
}
}
Output:
Array: [A, B, C]
List: [A, B, C]
9. Arrays.stream()
Creates a stream from the specified array.
Example:
import java.util.Arrays;
public class ArraysStreamExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = Arrays.stream(numbers).sum();
System.out.println("Sum of Array Elements: " + sum);
}
}
Output:
Sum of Array Elements: 15
10. Arrays.deepToString()
Converts a multi-dimensional array to a string representation.
Example:
import java.util.Arrays;
public class ArraysDeepToStringExample {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
String matrixString = Arrays.deepToString(matrix);
System.out.println("Matrix: " + matrixString);
}
}
Output:
Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
11. Arrays.deepEquals()
Checks if two multi-dimensional arrays are equal.
Example:
import java.util.Arrays;
public class ArraysDeepEqualsExample {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix2 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix3 = {
{7, 8, 9},
{10, 11, 12}
};
boolean isEqual = Arrays.deepEquals(matrix1, matrix2);
System.out.println("Matrix1 equals Matrix2: " + isEqual);
isEqual = Arrays.deepEquals(matrix1, matrix3);
System.out.println("Matrix1 equals Matrix3: " + isEqual);
}
}
Output:
Matrix1 equals Matrix2: true
Matrix1 equals Matrix3: false
12. Arrays.mismatch()
Finds and returns the index of the first mismatch between two arrays, otherwise returns -1 if no mismatch is found.
Example:
import java.util.Arrays;
public class ArraysMismatchExample {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 3, 4, 6};
int[] array3 = {1, 2, 3, 4, 5};
int mismatchIndex = Arrays.mismatch(array1, array2);
System.out.println("First mismatch between array1 and array2: " + mismatchIndex);
mismatchIndex = Arrays.mismatch(array1, array3);
System.out.println("First mismatch between array1 and array3: " + mismatchIndex);
}
}
Output:
First mismatch between array1 and array2: 4
First mismatch between array1 and array3: -1
Conclusion
The java.util.Arrays class provides a rich set of APIs for array manipulation, making it easier to perform common tasks such as sorting, searching, copying, and comparing arrays. By understanding and using these methods, you can effectively manage arrays in your Java programs, improving both the readability and efficiency of your code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment