java - How to use Arrays.asList() to do List functions on arrays -


prompt: given array of ints, return true if array contains 2 next 2 or 4 next 4, not both.

i have done arrays , no list methods, want way practice. here's have, arrays.aslist() giving me 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 using int instead of integer. new code:

public boolean either24(int[] nums)  {     integer[] nums2 = new integer[nums.length];     for(int = 0; < 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); } 

try this:

public boolean either24(int[] nums) {     return (arrays.tostring(nums).contains("2, 2") ^ arrays.tostring(nums).contains("4, 4"));        } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -