/********************************************************************* * Author: Eric Heim * Date Created: 6/16/2011 * Course: CS0007 * Description: Shows how to compare the elements of two arrays *********************************************************************/ public class ArrayComparison { public static void main(String[] args) { int[] firstArray = {1, 2, 3, 4, 5}; int[] secondArray = {1, 2, 3, 4, 5}; boolean arraysEqual = true; //Compares the references in the array variables if(firstArray == secondArray) { System.out.println("The references of the two array " + "variables are the same!"); } else { System.out.println("The references of the two array " + "variables are NOT the same!"); } //Compares the elements in the array variables if(firstArray.length != secondArray.length) { arraysEqual = false; } else { for(int i = 0; i < firstArray.length; i++) { if(firstArray[i] != secondArray[i]) { arraysEqual = false; break; } } } if(arraysEqual) System.out.println("The array's elements are the same"); else System.out.println("The array's elements are NOT the same"); } }