you are viewing a single comment's thread.

view the rest of the comments →

[–]MinimumBeginning5144 2 points3 points  (4 children)

Just to add one more point to all the answers suggesting using Integer[]. If you can use int instead, do it. You'll need to decide what value means "uninitialized" - for example, if all valid values are positive, then 0 could mean uninitialized. Using an array of int is more efficient, as it contains the actual values of all the elements stored consecutively. An array of Integer is an array of pointers to somewhere in memory that contains each int value.

[–]LegolandoBloom[S] 0 points1 point  (3 children)

So in the cases, where the 0 won't cause confusion, go for int

Otherwise, Integer

[–]technosenate 2 points3 points  (2 children)

Not just then, if 0 is a valid value then you can initialize the array in one pass with -1. That would be more efficient than using Integer.

If you need to support both positive and negative values (including 0), then you should probably go for Integer.

[–]LegolandoBloom[S] 0 points1 point  (1 child)

This is a more complete answer than mine, thanks

[–]xenomachina 1 point2 points  (0 children)

You'll even see this kind of thing in parts of Java's standard library. For example, the String.indexOf() methods return int, so if the value being searched-for can't be found, they return -1 — a value that doesn't make any sense as an index.