数组可以包含整数和浮点数吗
有人问我:java中的数组可以包含整数和浮点数吗?她从一位老师那里得到了这个问题。
现在我的答案是:是的,因为您可以声明一个对象数组并在其中存储整数和浮点数。
但现在我想知道这是否正确,因为从技术上讲,当您将 Integer 和 Float 对象存储在数组中时,它确实包含这两种类型,但如果您“询问”数组,他会告诉您他包含对象,并且如果我不进行簿记或类检查,则无法判断数组中有整数和浮点数。
另一方面,我仍然觉得这可能是正确的答案,因为理论上该数组包含这两种类型的对象。
所以我想征求一个聪明的意见:如果你被问到(在面试中,无论是测试中)在java中数组是否可以包含整数和浮点数,是还是否?你会回答什么?
Somebody asked me: can an array in java contain integers and floats? She got that question from a teacher.
Now my answer 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
float
的int
不适合Object[]
数组。但是,通过自动装箱,java 会将Float
或Integer
放入数组中。Float
和Integer
都扩展了Number
。所以你甚至可以制作一个数字数组Number[]
另外,你可以将
int
放入float[]
中,但是 java 会将 int 转换为 float。相反的方式也是可能的,但精度会丢失。 (编辑:即使从 int->float 精度也可能会丢失。float->int 可能会丢失有关值的总体大小的信息)。结论取决于问题。对于原始数据类型,数组不能包含其他数据类型。如果您使用对象数组(整数、浮点、数字),答案是肯定的。
A
int
offloat
does not fit into aObject[]
array. However, by autoboxing java will put aFloat
orInteger
into the array instead.Both
Float
andInteger
extendNumber
. So you can even make a array of numbersNumber[]
Also, you can put a
int
into afloat[]
, 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.
这不是一个有明确“是”或“否”答案的问题。
我可以看到可以用三种不同的方式来肯定地回答问题:
Number[]
类型,并且可以包含(引用)Float
和 <代码>整数对象;int[]
类型,并且可以存储int
as-as 和转换为int
的float
>使用Float.floatToRawIntBits
。在情况 3 中(也可以说在情况 2 中),您还需要一个并行数组来记录主数组每个元素中存储的值的类型。根据问题中内置的假设,这很可能会使这些答案失去资格。
如果这个问题在面试中出现,我会向面试官概述可能性,并询问他们正在寻找三种解释中的哪一种(如果有的话)。如果有必要的话,我会进一步阐述。
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:
Number[]
and could contain (references to)Float
andInteger
objects;double[]
and could containfloat
s andint
s cast todouble
(N.B. both casts are lossless);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.
如果你想使用原始数组,你可以使用
double[]
,因为double
可以存储所有可能的int
和float< /代码>值。唯一丢失的信息是该数字最初是
int
还是float
(有 3300 万个整数可以是其中之一)存储
int
double[] 中的 > 或float
检索
int
或float
值。如果您使用 Object[] 来存储对象的任意组合,包括整数和浮点数。
面试问题可以通过问受访者从未想过的问题来吸引他们,通常是因为没有充分的理由这样做(可能永远)如果问题听起来很奇怪,也许应该如此。 ;)
If you want to use a primitive array, you can use a
double[]
asdouble
can store all possibleint
andfloat
values. The only information lost is whether the number was originally anint
or afloat
(there are 33 million integers which can be either)To store an
int
orfloat
in a double[]To retrieve an
int
orfloat
value.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. ;)
在java中只有两种数据类型——对象(所有扩展Object类的东西)和基元(int、byte、char等)。此外,每个基元都有其对象同级 - 例如 int 和 java.lang.Integer
从技术上讲,数组只能包含对象。
但在 java 5.0 中,由于“自动装箱”功能,可以跳过原语到对象的转换 - 基本上它会替换每个调用,例如
<代码>
int myInt = 0;
数组[0] = myInt;
和
<代码>
数组[0] = new Integer(myInt);
这种替换是自动完成的,但是内部(在运行时)java 机器将具有带有对象(Integer)的数组,而不是带有基元(int)的数组,这可能会影响数组操作的性能。
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.