Somebody asked me: can an array in java contain integers and floats? She got that question from a teacher.
Now my an开发者_高级运维swer was: yes, since you can declare an array of objects and store integers and floats in it.
But now I'm wondering if that is correct, since technically when you store Integer and Float objects in an array, it kind of does contain both types, but if you would "ask" the array he would tell you he contains Objects, and if I don't do bookkeeping or class checks there's no way to tell that there are integers and floats in the array.
On the other hand I still feel it might be the right answer since theoretically the array contains objects of both types.
So I'm asking for a smart opinion: if you were asked (in an interview, a test whatever) wether in java an array can contain integers and floats, yes or no? What would you answer?
A int
of float
does not fit into a Object[]
array. However, by autoboxing java will put a Float
or Integer
into the array instead.
Both Float
and Integer
extend Number
. So you can even make a array of numbers Number[]
Also, you can put a int
into a float[]
, but java will then cast the int into a float. The other way around is also possible, but precision will be lost. (edit: Even from int->float precision can be lost. float->int may lose information about the overall magnitude of the value).
The conclusion would depend on the question. For primitive datatypes a array can not contain the other datatype. If you use a Object array (Integer, Float, Number) the answer would be yes.
This isn't a question that has a clear-cut "yes" or "no" answer.
I can see three distinct ways in which the question can be answered in the affirmative:
- the array could be of type
Number[]
and could contain (references to)Float
andInteger
objects; - the array could be of type
double[]
and could containfloat
s andint
s cast todouble
(N.B. both casts are lossless); - the array could be of type
int[]
and could storeint
s as-as andfloat
s converted toint
usingFloat.floatToRawIntBits
.
In case 3 (and arguably in case 2) you'd also need a parallel array that would record the type of the value stored in each element of the main array. Depending on the assumptions built into the question, this may well disqualify these as suitable answers.
If this question came up in an interview setting, I would outline the possibilities to the interviewer and ask which of the three interpretations, if any, they were looking for. If necessary, I would then elaborate further.
If you want to use a primitive array, you can use a double[]
as double
can store all possible int
and float
values. The only information lost is whether the number was originally an int
or a float
(there are 33 million integers which can be either)
To store an int
or float
in a double[]
double[] d =
int i =
float f =
d[0] = i;
d[1] = f;
To retrieve an int
or float
value.
int i = (int) d[0];
float f = (float) d[1];
If you use an Object[] to store any combination of Objects including Integer and Float.
Interview questions can catch out interviewees by asking them questions they have never thought about, usually because there was no good reason to do this (possibly ever) If the question sounds odd, perhaps it should. ;)
In java there are only two data types - objects (everything that extends class Object) and primitives (int, byte, char and so on). Also, every primitive have its object sibling - for example int and java.lang.Integer
Technically, array can contain only objects.
But in java 5.0 it's possible to skip primitive-to-object conversion thanks to 'autoboxing' functions - basically it's replacing each call like
int myInt = 0;
array[0] = myInt;
with
array[0] = new Integer(myInt);
This replacement is done automatically, however internally (in runtime) java machine will have array with objects (Integer), not with primitives (int), and this can affect performance of array operations.
精彩评论