Can an array contain integers and floats

Someone asked me: can an array in java contain integers and float? She received this question from the teacher.

Now my answer is: yes, since you can declare an array of objects and store integers and floats in it.

But now I wonder if this is correct, since technically, when you store Integer and Float objects in an array, it seems to contain both types, but if you β€œask” the array, it will tell you that it contains Objects, and if I do not do bookkeeping or class checking, there is no way to say that the array has integers and floating numbers.

On the other hand, I still feel that this might be the right answer, since theoretically the array contains objects of both types.

So, I ask for a reasonable opinion: if you were asked (in an interview, something in the tests), then in java the array can contain integers and float, yes or no? What would you answer?

+6
source share
4 answers

A int of float does not fit into the Object[] array. However, when autoboxing, java puts a float or Integer in the array.

Both float and Integer extend Number . So you can even make an array of numbers Number[]

Alternatively, you can put int in float[] , but java then converts int to float. Another method is also possible, but accuracy will be lost. (edit: Even from int-> float, accuracy may be lost. float-> int may lose information about the total value of the value).

The conclusion will depend on the question. For primitive data types, an array cannot contain another data type. If you use an Object array (Integer, Float, Number), the answer will be yes.

+15
source

This is not a question that has a clear yes or no answer.

I see three different ways that you can answer the question in the affirmative:

  • an array can be of type Number[] and may contain (references to) Float and Integer objects;
  • the array may be of type double[] and may contain Float and int cast from double (NB, both throws are lossless);
  • the array can be of type int[] and can store int as-as and Float converted to int using Float.floatToRawIntBits .

In case 3 (and, possibly, in case 2) you will also need a parallel array that will record the type of value stored in each element of the main array. Depending on the assumptions built into the question, this may well disqualify them as suitable answers.

If this question arose in an interview, I would set out the interviewer's options and ask which of the three interpretations, if any, they were looking for. If necessary, I will clarify later.

+2
source

If you want to use a primitive array, you can use double[] , because double can store all possible int and float values. The only lost information is whether the original number was int or float initially (there are 33 million integers that can be either)

To save int or float in double []

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

To get an int or float value.

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

If you use Object [] to store any combination of objects, including Integer and Float.

Interview questions can identify interviewees by asking questions that they never thought of, usually because there was no good reason for this (possibly, ever). If the question sounds strange, maybe it should .;)

+1
source

In java, there are only two types of data - objects (everything that extends the Object class) and primitives (int, byte, char, etc.). In addition, each primitive has its own related object - for example, int and java.lang.Integer

Technically, an array can only contain objects.

But in java 5.0, you can skip primitive conversion to an object thanks to the "autoboxing" function - basically it replaces every call, as
int myInt = 0; array[0] = myInt;
from
array[0] = new Integer(myInt);

This replacement is done automatically, but internally (at runtime) the Java machine will have an array with objects (Integer) and not with primitives (int), and this can affect the performance of operations with arrays.

0
source

Source: https://habr.com/ru/post/891094/


All Articles