Java Programing laungage

java.util Projects

java.util Project 1

Comparing Arrays

This section of tutorials we show that how to determine the given arrays are same or not. The given program illustrates you how to compare arrays according to the content of array:

Previous Home Next

In this section of the tutorials, you can see that the given program initializes two arrays and input five number from user through the keyboard. And then the program checks whether the given taken both arrays are same or not. This comparison operation is performed by using the equals() method of Arrays class.

Arrays.equals()

This method is used to compares two arrays.

Arrays is the class of the java.util.*; package. This class and it's methods are used for manipulating arrays.

Example


package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class comparearraytest {
public static void main(String[]args)throws IOException
	int[] array1 = new int[3];
int[] array2 = new int[3];
	  BufferedReader br = new BufferedReader
      (new InputStreamReader(System.in));
	  try{
		  



	  System.out.println("Enter the 3 numbers for the first Array : ");
	  for(int i = 0; i < array1.length; i++){
	  array1[i] = Integer.parseInt(br.readLine());
	  }
	  System.out.println("Enter the 3 numbers for the second Array : ");
	  for(int i = 0; i < array2.length; i++){
	  array2[i] = Integer.parseInt(br.readLine());
	  }
	  }
	  catch(NumberFormatException e){
	  e.printStackTrace();
	  }

	  boolean check = Arrays.equals(array1, array2);
	  if(check == false)
	  System.out.println("Not same.");
	  else
	  System.out.println("Both Arrays are same.");
	  }
}

Previous Home Next