int[] nums = {2,3,4};
//通过 Arrays.toString(nums) 转换成 string 后,存储格式为[2, 3, 4]
String num = Arrays.toString(nums);
下面看看 codingBat一道题:
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.
either24([1, 2, 2]) → trueeither24([4, 4, 1]) → trueeither24([4, 4, 1, 2, 2]) → false
Solution:
public boolean either24(int[] nums) {
String num = Arrays.toString(nums);
if((num.contains("2, 2") && !num.contains("4, 4")) ||
(!num.contains("2, 2") && num.contains("4, 4"))) {
return true;
}
return false;
}