top of page
Search
Writer's pictureAbhinaw Tripathi

Array Class Java


Array Class Arrays class provides methods to perform certain operations on any one dimensional array.All the methods of the array class are static ,So they can be called in the form of Arrays.methodname() . Arrays Class Methods

  • static void sort(array)

  • static void sort(array,int start,int end)

  • static int binarySearch(array,element)

  • static boolean equals(array1,array2)

  • static array copyOf(source-array,int n)

  • static void fill(array,value)

Program:

/**

*

*/

package com.collectionpack;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Arrays;

/**

* @author Abhinaw.Tripathi

*

*/

public class ArraysDemo

{

public static void main(String[] args) throws NumberFormatException, IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int arr[]=new int[5];

for(int i=0;i<5;i++)

{

System.out.println("Enter an Integer: ");

arr[i]=Integer.parseInt(br.readLine());

}

System.out.println("Contents of the Array");

display(arr);

Arrays.sort(arr);

System.out.println("The sorted array: ");

display(arr);

System.out.println("Which element to search?");

int element=Integer.parseInt(br.readLine());

int index=Arrays.binarySearch(arr, element);

if(index < 0)

System.out.println("Elemnt not found!!!");

else

System.out.println("Element is found at location: " +(index+1));

}

private static void display(int[] arr)

{

for(int i:arr)

System.out.println(i);

}

}

Output:

Enter an Integer:

2

Enter an Integer:

1

Enter an Integer:

3

Enter an Integer:

45

Enter an Integer:

52

Contents of the Array

2

1

3

45

52

The sorted array:

1

2

3

45

52

Which element to search?

3

Element is found at location: 3

3 views0 comments

Recent Posts

See All
bottom of page