Homework 12-19-2005 Lab Equal Arrays
public class Equality {
public Equality() {
}
public static void main(String[] args) {
int[] a={1,9,6,4,0,2,1,2};
int[] b={1,9,6,4,0,2,1,2};
if (a == b)
System.out.println("a and b are equal by ==.");
else
System.out.println("a and b are not equal by ==.");
System.out.println("== only tests memory addresses.");
if (equalArrays(a, b))
System.out.println(
"a and b are equal by the equalArrays method.");
else
System.out.println(
"a and b are not equal by the equalArrays method.");
System.out.println(
"An equalArrays method is usually a more useful test.");
}
public static boolean equalArrays(int[] a, int[] b)
{
if (a.length != b.length)
return false;
else
{
int i = 0;
while (i < a.length)
{
if (a[i] != b[i])
return false;
i++;
}
}
return true;
}
}
0 Comments:
Post a Comment
<< Home