I'm trying to write a code that returns a new array which swaps every pair of elements. If there is an odd number of elements, the last element remains the same.
Currently what I have is this which doesn't seem to be working at all. I keep returning a nullpointerexception error when trying to test it
public static String[] pairSwap(String[] inputArray) {
String[] outputArray = new String[inputArray.length];
for (int i = 0; i < inputArray.length; i += 2) {
inputArray[i] = outputArray[i + 1];
inputArray[i + 1] = outputArray[i];
if (inputArray.length % 2 != 0) {
inputArray[inputArray.length] = outputArray[inputArray.length];
}
}
return outputArray;
}