具有多种数据类型的 Java 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23957581/
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-14 09:24:37  来源:igfitidea点击:

Java Array with multiple data types

java

提问by Get Off My Lawn

What can I use to store multiple different types of data, Int/String/etc.? I come from a PHP background where I can store different types of data into an array, but I don't know how to do that in Java.

我可以用什么来存储多种不同类型的数据,Int/String/等?我来自 PHP 背景,我可以将不同类型的数据存储到数组中,但我不知道如何在 Java 中做到这一点。

Take this example:

拿这个例子:

$array = array(
    "val1" => 1,
    "val2" => "cat",
    "val3" => true
);

How can I make something similar in Java?

如何在 Java 中制作类似的东西?

采纳答案by Anubian Noob

Java is a strongly typed language. In PHP or Javascript, variables don't have a strict type. However, in Java, every object and primative has a strict type. You can store mutliple types of data in an Array, but you can only get it back as an Object.

Java 是一种强类型语言。在 PHP 或 Javascript 中,变量没有严格的类型。然而,在 Java 中,每个对象和原始类型都有一个严格的类型。您可以在数组中存储多种类型的数据,但只能将其作为对象取回。

You can have an array of Objects:

您可以拥有一组对象:

Object[] objects = new Object[3];
objects[0] = "foo";
objects[1] = 5;

Note that 5 is autoboxed into new Integer(5)which is an object wrapper around the integer 5.

请注意, 5 是自动装箱的new Integer(5),它是一个围绕整数 5 的对象包装器。

However, if you want to get data out of the array, you can only get it as an Object. The following won't work:

但是,如果要从数组中获取数据,则只能将其作为对象获取。以下将不起作用:

int i1 = objects[1]; // Won't work.
Integer i2 = objects[2]; // Also won't work.

You have to get it back as an Object:

您必须将其作为对象取回:

Object o = objects[0]; // Will work.

However, now you can't get back the original form. You could try a dangerous cast:

但是,现在您无法取回原始表格。你可以尝试一个危险的演员:

String s = (String) o;

However you don't know that ois a String.

但是你不知道这o是一个字符串。

You can check with instanceof:

您可以检查instanceof

String s = null;

if (o instanceof String)
    s = (String) o;

回答by Rarw

You could use an object array but that creates problems when the time comes to retrieve the objects you have stored. Instead I would use a typesafe heterogenous containeras described in Effective Java (and linked to earlier in this sentence).

您可以使用对象数组,但是当需要检索您存储的对象时会产生问题。相反,我会使用有效 Java 中描述的类型安全的异构容器(并链接到这句话的前面)。

public class DateStuff{

    private Map<Class<?>, Object> dateMap =
        new HashMap<Class<?>, Object>();

    public <T> void putDate(Class<T> type, T instance){
          if(type == null)
              throw new NullPointerException("Type null");
          dateMap.put(type, instance);
    } 

    public<T> getDate(Class<T> type){
          return type.cast(dateMap.get(type));
    }

}

The typesafe heterogenous container solves the problem of retrieving objects later by mapping objects by their class. In your case I would combine this with other data structures - for example List<Date>, List<String>, or List<Integer>, as the base classes to provide a way to store multiple different kinds of objects in one collection. Then to retrieve values you would simply get the sub collection, e.g. a List<Date>, knowing that all items contained therein were of the same class.

类型安全的异构容器通过按对象的类映射对象来解决以后检索对象的问题。在您的情况下,我会将其与其他数据结构结合使用 - 例如List<Date>, List<String>, or List<Integer>,作为基类,以提供一种在一个集合中存储多种不同类型对象的方法。然后要检索值,您只需获取子集合,例如 a List<Date>,知道其中包含的所有项目都属于同一类。