I'm supposed to make a method to test if two arrays have the same values in reverse order.
public static boolean areIdentical(int[] m, int[] n)
{
boolean identical = true;
if(m.length != n.length) identical = false;
if(identical)
{
for(int x = 0; x < m.length; x++)
{
if(m[x] != n[x])
{
identical = false;
}
}
}
return identical;
//write a loop that compares each individual element of the two arrays.
//if a mismatch is found, return false
}
I test it by running it through these if statements with the following pre-determined arrays
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int[] b = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] c = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] d = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] e = {1, 2, 3, 4, 5, 4, 3, 2, 1};
int[] f = {1, 2, 3, 4, 4, 3, 2, 1};
int[] g = {1, 3, 5, 7, 9, 0, 2, 4, 6, 8};
int[] h = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int[] i = {1, 1, 3, 3, 5, 5, 7, 7, 9, 9};
//test for areReversed, uncomment the next two lines to test your method
if(areReversed(a, b) && !areReversed(a, g) && !areReversed(a, c)) System.out.println("Basic areReversed method test PASSED");
else System.out.println("Basic areReversed method test FAILED");
My method doesn't pass the test, am I making an oversight somewhere in my code?
Aucun commentaire:
Enregistrer un commentaire