Unit 6 Homework
// Write array methods for the Array Methods class below out of the options given above.
//Swap the first and last element in the array
import java.util.Arrays;
public class ArrayMethods {
public static void main(String[] args)
{
int[] array_nums = {1, 2, 3};
System.out.println("Original Array: "+Arrays.toString(array_nums));
int x = array_nums[0];
array_nums[0] = array_nums[array_nums.length-1];
array_nums[array_nums.length-1] = x;
System.out.println("New Array: "+Arrays.toString(array_nums));
}
}
ArrayMethods.main(null);
// Replace all even elements with 0
public static int replaceEvenDigitsWithZero(int number) {
return (number%2 == 0 ? 0 : number % 10) + (number<10 ? 0 : 10 * replaceEvenDigitsWithZero(number / 10));
}