java.util.Arrays class

Previous
Next

Java.util.Arrays class:

  • This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
  • The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.
    • public class Arrays extends Object

static int binarySearch​(byte[] a, byte key) : Searches the specified array of bytes for the specified value using the binary search algorithm.

import java.util.Arrays;
class Demo 
{
	public static void main(String[] args) 
	{
		byte arr[ ] = {10,20,30,40,50};

		int result = Arrays.binarySearch(arr, (byte)30);
		System.out.println("Found at : " + result);
	}
}

public static void sort​(byte[] a) : Sorts the specified array into ascending numerical order.

import java.util.Arrays;
class Demo 
{
	public static void main(String[] args) 
	{
		byte arr[ ] = {8,2,9,1,5,7,2,8,0};

		Arrays.sort(arr);
		System.out.println("Sorted Array elements are : ");
		for (int i=0 ; i<arr.length ; i++)
		{
			System.out.println(arr[i]);
		}
	}
}

static int mismatch(int[] a, int[] b) : Finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found.

static int hashCode(int[] a) : Returns a hash code based on the contents of the specified array.

static void fill(byte[] a, byte val) : Assigns the specified byte value to each element of the specified array of bytes.

Previous
Next

Add Comment

Courses Enquiry Form