How to handle array element between int and Integer
First, it is long post so if you need clarification please let me know.
I'm new to Java and having difficulty deciding whether I should use int[] or Integer[]. I wrote a function that find odd_number from int[] array.
public int[] find_odd(int[] arr) {
int[] result = new int[arr.length];
for(int i=0; i<arr.length; i++) {
if(arr[i] % 2 != 0) {
//System.out.println(arr[i]);
result[i] = arr[i];
}
}
return result;
}
Then, when I pass the int[] array consisting of some integer like below:
int[] myArray = {-1, 0, 1, 2, 3};
int[] result = find_odd(myArray);
The array "result" contains:
0, -1, 0, 1, 0, 3
Because in Java you have to define the size of array first, and empty int[] array element is 0 not null. So when I want to test the find_odd() function and expect the array to have odd numbers (which it does) only, it throws the error because the array also includes 0s representing "empty cell" as shown above.
My test code:
public void testFindOddPassValidIntArray() {
int[] arr = {-2, -1, 0, 1, 3};
int[] result = findOddObj.find_odd(arr);
//check if return array only contain odd number
for(int i=0; i<result.length; i++) {
if(result[i] != null) {
assert is_odd(result[i]) : result[i];
}
}
}
So, my question is should I use int[] array and add check for 0 element in my test like:
if(result[i] != 0) {
assert is_odd(result[i] : result[i]
}
But in this case, if find_odd() function is broken and returning 0 by miscalculation, I can't catch it because my test code would only assume that 0 is empty cell.
OR should I use Integer[] array whose default i开发者_StackOverflows null? How do people deal with this kind of situation?
My advice is generally you shouldn't use arrays. You should use List
s instead. At that point, the choice is made for you. You have to use Integer
not int
because Java generics don't support primitive types (unlike, say, C#).
public List<Integer> findOdd(List<Integer> in) {
List<Integer> ret = new ArrayList<Integer>();
for (Integer i : in) {
if (i % 2 == 1) {
ret.add(i);
}
}
return ret;
}
Since you're new to Java, this may introduce a few new concepts to you.
Java Collections don't have the same limitations as arrays. For example, you don't have to declare the size of an ArrayList
when you create it. Array allocation is abstracted away by this particular class.
Now all this assumes you don't want the result array to be the same size as the input array. There are valid reasons for this (eg when doing vector/matrix maths). The two ways of handling this kind of thing are to use Integer
(so you can put null
for empty values but this then requires you to test for null
) or by using a sentinel value (eg Integer.MAX_VALUE
).
Two things.
First, (0%2 == 0) is true, so if 0 is present in 'arr' it will output as an odd number. You'll want something like:
if(arr[i] != 0 && arr[i] % 2 != 0)
Second, you don't want to return an array of the same size, so you would be better suited using a List<Integer> as that can grow to the size you need at runtime, but just because you are using Integer does not mean you should fill your list with NULLs, instead you should just not add anything to the List if it doesn't pass your test for "odd".
If in your scenario it's valid for the array to have "empty" cells then you need some way of representing them. An Integer
array with null
values is certainly the more obvious and clear way.
In the rare case that you know that a "non-empty" cell will never contain 0 and performance is critical you could use 0 to represent "empty" - but I'd think twice before I do this.
You can use an Integer[]
array instead of an int[]
array, which will trivially allow you to use null
.
This works as int
is a built-in primitive type, while Integer
is instead a reference to some integer in memory. Java will silently convert between the two via boxing and unboxing.
The downside is that you will get performance degradation due to the boxing and unboxing -- but this is something you don't need to worry about unless the algorithm is working on large sets of data or is real-time.
The method public int[] find_odd(int[] arr) should only return the odd values and shouldn't return "empty" spaces. This would solve your problem as there would be no empty spaces to check for.
public int[] find_odd(int[] arr) {
int[] result = new int[arr.length];
int results = 0;
for(int i=0; i<arr.length; i++)
if(arr[i] % 2 != 0)
result[results++] = arr[i];
int[] result2 = new int[results];
System.arraycopy(result, 0, result2, 0, results);
return result2;
}
The array "result" contains:
-1, 1, 3
An alternative to using an int[] is the TintArrayList from GNU Trove. It has much of the efficiency of an int[] and much of the convenience of an ArrayList
精彩评论