Java 如何声明不同数据类型的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16363547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:30:52  来源:igfitidea点击:

How to declare an array of different data types

javaarrays

提问by srk

I am working with arrays in Java and I have got a question. I know that an array in Java is a collection of similar data types, as shown below:

我正在使用 Java 中的数组,我有一个问题。我知道Java中的数组是相似数据类型的集合,如下图:

int[] x = new int[]{1,2,3};

The above declaration can be read as an Integerarray which is a collection of integer types.

上面的声明可以理解为一个Integer数组,它是一个整数类型的集合。

Consider this:

考虑一下:

Object[] x = new Object[]{1,2,3,"srk"};

Here, can I say that the above is an array which is a collection of dis-similar data types, or is it an Objectarray of similar data types, i.e. objects?

在这里,我可以说上面是一个数组,它是一个不同数据类型的集合,还是一个Object类似数据类型的数组,即对象?

I am muddled and skeptical about this. In Java, is it possible to create an array or any sort of collection which can hold different data types?

我对此感到困惑和怀疑。在 Java 中,是否可以创建一个数组或任何可以保存不同数据类型的集合?

采纳答案by M Sach

ALL objects in Java extend Object.

Java 中的所有对象都扩展了 Object。

Therefore it is possible to be completely non-descriptive when you create the array by declaring it an array of Objects:

因此,当您通过将数组声明为对象数组来创建数组时,可能完全是非描述性的:

Object[] arr = new Object[6];

This code creates an array of objects of length 6.

这段代码创建了一个长度为 6 的对象数组。

So for instance, you could create an array where entries come in pairs of two. In this case, the first object is a String and the second is an Integer.

因此,例如,您可以创建一个数组,其中条目成对出现两个。在这种情况下,第一个对象是一个字符串,第二个是一个整数。

Object[] arr = new Object[6];

arr[0] = new String("First Pair");
arr[1] = new Integer(1);
arr[2] = new String("Second Pair");
arr[3] = new Integer(2);
arr[4] = new String("Third Pair");
arr[5] = new Integer(3);

Now if you want to actually figure out what these objects are then it will require a cast:

现在,如果您想真正弄清楚这些对象是什么,则需要进行强制转换:

int x = (Integer)arr[1];

回答by Mike Samuel

In java, is it possible to create an array or any sort of collection which can hold different data types?

在 java 中,是否可以创建一个数组或任何类型的可以保存不同数据类型的集合?

Yes.

是的。

"Heterogeneous collection" is the term most often used for this, and
what is the point of heterogenous arrays?discusses them in java.

“异构集合”是最常用的术语
,异构数组的意义何在?在java中讨论它们。

For collection types, you can use List<Object>to which you can add many kinds of objects, and List<?>which can receive many kinds of lists but which you cannot add to because of type variance.

对于集合类型,您可以使用List<Object>which 可以添加多种对象,并且List<?>可以接收多种列表但由于类型变化而无法添加。



The variance of Java arrays is a little odd though because

Java 数组的变化有点奇怪,因为

Object[] arr = new String[3];  // OK
List<Object> list = new ArrayList<String>();  // DOES NOT COMPILE

arr[0] = Integer.valueOf(42);  // RUNTIME ERROR

so when you see an Object[]you need to know that it was created via new Object[]for it to be safe to use as a heterogenous array. This is unlike a Collection<Object>where the type-system gives you some degree of safety.

因此,当您看到一个时,Object[]您需要知道它是通过new Object[]它创建的,以便可以安全地用作异构数组。这与Collection<Object>类型系统为您提供一定程度的安全不同。

回答by Evgeniy Dorofeev

In Java you can create an array of Objects

在 Java 中,您可以创建一个对象数组

Object[] x = new Object[10];

and you can assign references to instances of any class to its elements since any class in Java is an Object.

并且您可以将任何类的实例的引用分配给其元素,因为 Java 中的任何类都是对象。

But it is different with primitive arrays. int[]can contain only intelements, long[]only longs, etc

但是原始数组就不同了。int[]只能包含int元素,long[]只能包含longs 等

回答by Kuchi

It works exactly how you thought:

它完全按照您的想法工作:

Object[] x = new Object[]{1,2,3,"srk"};
for(Object o: x){
   System.out.println(o.getClass());
}

Output:

输出:

class java.lang.Integer
class java.lang.Integer
class java.lang.Integer
class java.lang.String

回答by Varad

Creating an Object[] array is one way to do it. Otherwise, you can create a class with the variables you want, and have an array of that class's objects.

创建 Object[] 数组是一种方法。否则,您可以使用所需的变量创建一个类,并拥有该类对象的数组。

class MyClass{
    int var_1;
    String var_2;
    ...
    ...
}
...
MyClass[] arr = new MyClass[3];

Then, use arr where you want to.

然后,在您想要的地方使用 arr。

回答by Griffin

To add to the other answers, you can put whatever you want in an array of Objects. But if you wish to access any of methods or properties, not shared with Object, that a specific element has, then you have to down-cast itto the needed type as Java will recognise it as type Object- this is something you have to be careful with.

要添加到其他答案中,您可以将所需的任何内容放入对象数组中。但是,如果您希望访问Object特定元素具有的任何未与 共享的方法或属性,那么您必须将其向下转换为所需的类型,因为 Java 会将其识别为类型Object- 这是您必须小心的事情和。

Example:

例子:

    Object test[];
    test = new Object[]{1, 2, "three", new Date()};
    System.out.println( ( (Date)test[3] ).getMonth() );
    // the above line will output '4', but there will be a compilation error
    // if the cast (Date) is emitted