Java Array Methods10 Sept 2024 | 8 min read Java's Arrays class in the java.util package offers a range of static methods facilitating operations on arrays. It provides functionality to fill, sort, search, and more. The methods enhance array manipulation, contributing to cleaner and more efficient code. Let's examine the operations provided by the Arrays class by reviewing its methods presented below in a tabular format.
Implementation:Example 1In the following program, we have used the asList(), binarySearch(), compare(), and the compareUnsigned() method. Filename: ArrayMethodsExample1.java Output: Original Array: [1, 4, 7, 10, 13] Index of 7: 2 Index of 10 in range [1, 4]: 3 Comparison result between array1 and array2: 0 Unsigned comparison result between unsignedArray1 and unsignedArray2: 1 Example 2In the following program, we have used the copyOf(), copyOfRange(), deepEquals(), deepHashCode(), and deepToString() method. Filename: ArrayMethodsExample2.java Output: Copy of originalArray with new length: [1, 2, 3, 4, 5, 0, 0] Copy of originalArray from index 1 to 4: [2, 3, 4] Are array1 and array2 deepEquals? true Deep hash code of array1: 30848 Deep string representation of array1: [[1, 2, 3]] Example 3In the following program, we have used the equals(), fill(), hashCode(), mismatch(), and parallelSort() method. Filename: ArrayMethodsExample3.java Output: Are array1 and array2 equal? true Filled array with value 7: [7, 7, 7, 7, 7] Hash code of the array: 35309286 Mismatch index between mismatchArray1 and mismatchArray2: 3 Sorted array using parallelSort: [1, 2, 3, 4, 5] Example 4In the following program, we have used the various types of the sort() method and the spliterator method. Filename: ArrayMethodsExample4.java Output: Sorted array using sort(): [1, 2, 3, 4, 5] Partially sorted array from index 1 to 4: [5, 1, 3, 4, 2] Sorted string array by length: [Apple, Banana, Orange, Grapes] Sorted array in reverse order: [5, 4, 3, 2, 1] Spliterator characteristics: 17488 Example 5In the following program, we have used the spliterator(), stream(), and the toString() method. Filename: ArrayMethodExample5.java Output: Spliterator characteristics: 17488 IntStream from originalArray: 1 2 3 4 5 String representation of originalArray: [1, 2, 3, 4, 5] Next Topicjava.lang.Class class in Java |
Java is a powerful programming language known for its versatility and extensive libraries. When working with arrays, you may often encounter scenarios where you need to calculate the sum of two arrays. Whether you're a beginner or an experienced developer, understanding how to accomplish this task...
5 min read
A function of the CharsetDecoder class, the isCharsetDetected() method in Java, determines if the charset of a given input has been identified properly when utilizing a decoder that enables auto-detection. When using this method by default, an UnsupportedOperationException is always thrown. Auto-detecting decoders should override it...
3 min read
Java is a popular object-oriented programming language that is used to create powerful and efficient software applications. In Java, a class is the fundamental unit of code, and it defines the blueprint for objects. Every object in Java belongs to a class, and a class consists...
7 min read
It is similar to the other iterators available in Java to traverse the elements of the source(Collection, Generator function or IO channel). Spliterator is a base utility for Streams, especially parallel ones. In order to use Spliterator for collections, we create an object of Spliterator by calling...
9 min read
Before the development of computer or programming, people did their jobs manually. It used to take a lot of time but they had no choice. Then the computer era came, and now the jobs to be done were fed on the system. It considerably reduced...
2 min read
In the ious sections, we have discussed many pattern programs. In this section, we will create Java program to print ladder with n steps. Using for Loop The following program prints the ladder with the gap between two side rails being 3 spaces. LadderPatternExample1.java import java.util.Scanner; public class LadderPatternExample1 { public static void...
2 min read
? There are different versions of Java is available. Some of the applications generally require different version because of compatibility problems. In this section, we will learn how to check JRE version in Windows using CMD. A version string contains a version number optionally followed by pre-release and...
2 min read
How to Convert a String to String Array in Java? In Java, a String is an object that represents the sequence of characters. In order to use Strings, we need to import the String class defined in java.lang package. The String array is an array of strings...
6 min read
An integer 'N' is given. Our task is to find out the total number of binary strings whose size is equal to N such that the strings do not contain consecutive 1's. Example 1: Input: int N = 4 Output: 8 Explanation: For N equal to 4, we have the following...
9 min read
The Java programming language is a platform-independent language (WORA) because it does not depend on any platform type. When a Java code is compiled, it is compiled into byte code through JIT (Just-in-Time) compiler, and the byte code is independent of the platform. To execute the...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India