I'm trying to retrieve the item at the specified index in an array as shown in the body of the forloop.
public int arrayCount9(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i ++) {
if (Array.get(nums, i) == 9) {
count ++;
}
}
return count;
}
However, the correct code in this problem is actually this:
public int arrayCount9(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i ++) {
if (nums[i] == 9) {
count ++;
}
}
return count;
}
Is there a difference between Array.get(nums, i) == 9) and (nums[i] == 9)? The documentation for get(Object array,int index) seems suitable for this problem. In addition, I've actually never encountered (nums[i] == 9) before so if you could explain that code as well, much appreciated!
Array
exists. What the heck is the purpose of it?Class
objects? I have to agree that most of the methods seem uselessget
method works with arrays ofObject
s. UsegetInt
instead if you have to. However, I don't understand why you simply can't use the second approach withnums[i]
Array
is the class you'd use when working with the Reflection API and don't want to cast.