I have created a generic swapping algorithm. In order to for this an array is required and the elements are swapped using integer indices.
I have created a JUnit test class for this and cannot figure out what to use instead of assertArrayEquals if the output should be an error.
Here is my attempt:
@Test
public void testNull() {
GenericMethods gm = new GenericMethods();
String names[] = null;
Error err = null;
//String expectedOutput[] = { "Hugh", "Simon", "Ebrahim", "Diane", "Paula", "Andrew" };
assertArrayEquals(gm.swap(names, 1, 5), (err));
}
Here is the Generic swap method used
package genericMethods;
import java.util.Arrays;
public class GenericMethods {
public static <T> T[] swap(T[] names, int index1, int index2) {
if(names == null){
return null; //This will return nothing if nothing has been selected
}
if ((index1 >= 0 && index1 < names.length) && (index2 >= 0 && index2 < names.length)) {
T string = names[index1];
names[index1] = names[index2];
names[index2] = string; //Will check if a valid name has been selected
}
return names;
}
public static void main(String[] args) {
String[] names = new String[] { "Hugh", "Andrew", "Ebrahim", "Diane", "Paula", "Simon" };
swap(names, 1, 5); //will swap Hugh with Simon
}
}