数组可以包含整数和浮点数吗

发布于 2024-11-16 20:56:55 字数 341 浏览 3 评论 0原文

有人问我: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你又不是我 2024-11-23 20:56:55

floatint 不适合 Object[] 数组。但是,通过自动装箱,java 会将 FloatInteger 放入数组中。

FloatInteger 都扩展了 Number。所以你甚至可以制作一个数字数组 Number[]

另外,你可以将 int 放入 float[] 中,但是 java 会将 int 转换为 float。相反的方式也是可能的,但精度会丢失。 (编辑:即使从 int->float 精度也可能会丢失。float->int 可能会丢失有关值的总体大小的信息)。

结论取决于问题。对于原始数据类型,数组不能包含其他数据类型。如果您使用对象数组(整数、浮点、数字),答案是肯定的。

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.

定格我的天空 2024-11-23 20:56:55

这不是一个有明确“是”或“否”答案的问题。

我可以看到可以用三种不同的方式来肯定地回答问题:

  1. 数组可以是 Number[] 类型,并且可以包含(引用)Float 和 <代码>整数对象;
  2. 该数组可以是 double[] 类型,并且可以包含 float 和 int 转换为 double (注意两种转换都是无损);
  3. 该数组可以是 int[] 类型,并且可以存储 int as-as 和转换为 intfloat >使用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:

  1. the array could be of type Number[] and could contain (references to) Float and Integer objects;
  2. the array could be of type double[] and could contain floats and ints cast to double (N.B. both casts are lossless);
  3. the array could be of type int[] and could store ints as-as and floats converted to int using Float.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.

萌化 2024-11-23 20:56:55

如果你想使用原始数组,你可以使用double[],因为double可以存储所有可能的intfloat< /代码>值。唯一丢失的信息是该数字最初是 int 还是 float(有 3300 万个整数可以是其中之一)

存储 int double[] 中的 > 或 float

double[] d = 
int i = 
float f = 
d[0] = i;
d[1] = f;

检索 intfloat 值。

int i = (int) d[0];
float f = (float) d[1];

如果您使用 Object[] 来存储对象的任意组合,包括整数和浮点数。

面试问题可以通过问受访者从未想过的问题来吸引他们,通常是因为没有充分的理由这样做(可能永远)如果问题听起来很奇怪,也许应该如此。 ;)

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. ;)

把时间冻结 2024-11-23 20:56:55

在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文