Compare Two Java int Arrays Example

  1. /*
  2.   Compare Two Java int Arrays Example
  3.   This java example shows how to compare two integer arrays for equality using
  4.   Arrays.equals method.
  5. */
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class CompareIntArraysExample {
  10.  
  11. public static void main(String[] args) {
  12. //create int arrays
  13. int[] intArray1 = new int[]{27,78,1023};
  14. int[] intArray2 = new int[]{27,78,1023};
  15.  
  16. /*
  17.   To compare two int arrays use,
  18.   static boolean equals(int array1[], int array2[]) method of Arrays class.
  19.  
  20.   It returns true if both arrays are equal. Arrays are considered as equal
  21.   if they contain same elements in same order.
  22.   */
  23.  
  24. boolean blnResult = Arrays.equals(intArray1,intArray2);
  25. System.out.println("Are two int arrays equal ? : " + blnResult);
  26.  
  27. /*
  28.   Please note that two int array references pointing to null are
  29.   considered as equal.
  30.   */
  31.  
  32. }
  33. }
  34.  
  35. /*
  36. Output of the program would be
  37. Are two int arrays equal ? : true
  38. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

what the answer of this.

This program will ask the user to enter his choice.
If the user enters 1 meaning you are going to add the first number to the second number.
Hint: pls take note that everything you typed in using keyboard is automatically converted to String. Since you can’t add string to another string. You need to convert the first number to integer equivalent e.g.

  1.  
  2. int firstNumber =0;
  3. int secondNumber =0;
  4.  
  5. String Fnumber = r.readline();
  6. String Snumber = r.readline();
  7.  
  8. firstNumber = Integer.parseInt(Fnumber);
  9. secondNumber = Integer.parseInt(Snumber);
  10.  
  11. int sum = firstNumber + secondNumber;
  12.  
  13. sop (“ The sum is :+ sum);
  14.  

(1) Add
(2) Subject
(3) Multiply
(4) Divide
(5) Quit

Enter your choice: 2

Enter First number : 5
Enter Second number: 4
The difference is : 1

(6) Add
(7) Subject
(8) Multiply
(9) Divide
(10) Quit

Enter your choice: 1

Enter First number : 3
Enter Second number: 4
The sum is : 7

(1) Add
(2) Subject
(3) Multiply
(4) Divide
(5) Quit

Enter your choice: 3

Enter First number : 3
Enter Second number: 4
The sum is : 12

Enter your choice: 5


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!