Java Sorting Algorithms:
Reverse an array in java:
for(int i = 0; i < validData.length/2; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
Checking to see if one array is the reverse of another array in java:
public static boolean checkReversed(int[] x, int[] y)
{
// For production code, possibly add nullity checks here
//(see comments)
if (x.length != y.length)
{
return false;
}
// Loop through x forwards and y backwards
for (int i = 0; i < x.length; i++)
{
if (x[i] != y[y.length - 1 - i])
{
// As soon as we've found a "mistake" we can exit:
// This is simpler (IMO) than keeping a "check" variable
return false;
}
}
return true;
}
No comments:
Post a Comment