Back to the Program-A-Day homepage
Problem
This problem envolves you seeing if two hard coded arrays are equal. You should use two int arrays to equate. Even though your arrays are hard coded into your program, you should not assume to know the length of the arrays. In other words use: array.length
|
|
...By Students
|
Solution
The solution...
|
Code
Solution Code
public class ArrayEquate
{
public static void main (String[]args)
{
int[] array1 = {1,2,3,4,5,6,7,8,9,10};
int[] array2 = {1,2,3,4,5,6,7,8,9,0};
boolean equal = true;
if (array1.length == array2.length)
{
for (int i = 0; i < array1.length; i++)
{
if (array1[i] != array2[i])
equal = false;
}
}
else
equal = false;
if (equal)
System.out.println ("The two arrays are equal.");
else
System.out.println ("The two arrays are not equal.");
}
}
|
Back to the Program-A-Day homepage