Download java util Arrays Class in Formats
QR Code for this Page
java util Arrays Class

java util Arrays Class

java.util.Arrays Class

Arrays Class in Java

Java Arrays class contains various methods for manipulating arrays (such as sorting and searching). Java Arrays class contains a static factory that allows arrays to be viewed as lists. You need not to instantiate java.util.Arrays class because all the methods are static methods so that we can call the methods with the class name. The methods in this class all throw a NullPointerException if the specified array reference is null, except where noted.

In this tutorial you can learn about java.util.Arrays class and its examples. And also learn how to use java.util.Arrays class. Following example shows how to use int array with java.util.Arrays class.

How to use int array with java.util.Arrays class Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*  How to use int array with java.util.Arrays class Example
    Save with file name ArraysExample.java  */
  
import java.util.Arrays;
  
public class ArraysExample
{
  public static void main(String args[])
  {
    //int ARRAY DECLARATION AND CREATION
    int arr[] = {0,-5,2,5,-1,-4,-3,-2,3,1,4};
    //int ARRAY OUTPUT AS STRING
    System.out.println(Arrays.toString(arr));
    //int ARRAY SORTING
    Arrays.sort(arr);
    //int ARRAY OUTPUT AS STRING AFTER SORTING
    System.out.println("After Sorting");
    System.out.println(Arrays.toString(arr));
    //SEARCHING IN int ARRAY
    int index = Arrays.binarySearch(arr, 3);
    if( index >= 0 )
      System.out.println("3 is Found at Index : " + index);
    else
      System.out.println("3 is NOT Found");
    //FILLING int ARRAY WITH DEFAULT ZERO
    Arrays.fill(arr,0);
    //int ARRAY OUTPUT AFTER FILL
    System.out.println(Arrays.toString(arr));
  }
}

Following example shows how to use double array with java.util.Arrays class.

How to use double array with java.util.Arrays class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*  How to use double array with java.util.Arrays class
    Save with file name ArraysExample2.java */
  
import java.util.Arrays;
  
public class ArraysExample2
{
  public static void main(String args[])
  {
    //double ARRAY DECLARATION AND CREATION
    double arr[] = {0.0,-5.0,2.0,5.0,-1.5,-4.0,-3.0,-2.0,3.0,1.5,4.0};
    //double ARRAY OUTPUT AS STRING
    System.out.println(Arrays.toString(arr));
    //double ARRAY SORTING
    Arrays.sort(arr);
    //double ARRAY OUTPUT AS STRING AFTER SORTING
    System.out.println("After Sorting");
    System.out.println(Arrays.toString(arr));
    //SEARCHING IN double ARRAY
    int index = Arrays.binarySearch(arr, 3);
    if( index >= 0 )
      System.out.println("3 is Found at Index : " + index);
    else
      System.out.println("3 is NOT Found");
    //FILLING double ARRAY WITH DEFAULT ZERO
    Arrays.fill(arr,0);
    //double ARRAY OUTPUT AFTER FILL
    System.out.println(Arrays.toString(arr));
  }
}
Note :

When filling an object array, the fill() method will copy the same object reference to every cell in the array. After filling the array, if you then change that one reference, the attributes of the object in every cell in the array will be changed.

Following example shows how to use String array with java.util.Arrays class.

How to use String array with java.util.Arrays class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*  How to use String array with java.util.Arrays class
    Save with file name ArraysExample3.java */
  
import java.util.Arrays;
import java.util.Collections;
  
public class ArraysExample3
{
  public static void main(String args[])
  {
    //String ARRAY DECLARATION AND CREATION
    String arr[] = {"Java Tutorials","NetBeans Tutorials","Huda Tutorials","C"};
    //String ARRAY OUTPUT AS STRING
    for(int i=0;i<arr.length; i++)
    {
      System.out.println(arr[i]);
    }
    //String ARRAY SORTING
    Arrays.sort(arr);
    //String ARRAY OUTPUT AS STRING AFTER SORTING
    System.out.println("\nAfter Sorting\n");
    for(int i=0;i<arr.length; i++)
    {
      System.out.println(arr[i]);
    }
    //SEARCHING IN String ARRAY
    int index = Arrays.binarySearch(arr, "Huda Tutorials");
    if( index >= 0 )
      System.out.println("\nHuda Tutorials is Found at Index : " + index);
    else
      System.out.println("\nHuda Tutorials is NOT Found");
    //String ARRAY REVERSE ORDER
    Arrays.sort(arr,Collections.reverseOrder());
    //String ARRAY OUTPUT AS STRING AFTER SORTING
    System.out.println("\nAfter Reverse Order\n");
    for(int i=0;i<arr.length; i++)
    {
      System.out.println(arr[i]);
    }
    //FILLING String ARRAY WITH DEFAULT null
    Arrays.fill(arr,null);
    //String ARRAY OUTPUT AFTER FILL
    System.out.println(Arrays.toString(arr));
  }
}
Download java util Arrays Class in Formats