Prompt: Given an array of ints, return true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both.
I have done this with just Arrays and no List methods, but I want to do it this way for practice. Here's what I have, Arrays.asList() is giving me some grief.
public boolean either24(int[] nums)
{
List list = Arrays.asList(nums);
boolean twos = list.containsAll(Arrays.asList(2, 2));
boolean fours = list.containsAll(Arrays.asList(4, 4));
return (twos || fours) && !(twos && fours);
}
Expected Run
either24({1, 2, 2}) → true true OK
either24({4, 4, 1}) → true true OK
either24({4, 4, 1, 2, 2}) → false false OK
either24({1, 2, 3, 4}) → false false OK
either24({3, 5, 9}) → false false OK
either24({1, 2, 3, 4, 4}) → true false X
either24({2, 2, 3, 4}) → true false X
either24({1, 2, 3, 2, 2, 4}) → true false X
either24({1, 2, 3, 2, 2, 4, 4}) → false false OK
either24({1, 2}) → false true X
either24({2, 2}) → true true OK
either24({4, 4}) → true true OK
either24({2}) → false true X
either24({}) → false false OK
UPDATE: Part of problem was using int instead of Integer. New code:
public boolean either24(int[] nums)
{
Integer[] nums2 = new Integer[nums.length];
for(int i = 0; i < nums.length; i++)
nums2[i] = nums[i];
List list = Arrays.asList(nums2);
boolean twos = list.containsAll(Arrays.asList(2, 2));
boolean fours = list.containsAll(Arrays.asList(4, 4));
return (twos || fours) && !(twos && fours);
}