在Java中将多个对象存储在单个数组元素中

huangapple go评论73阅读模式
英文:

Storing multiple objects in a single array element in Java

问题

可以在单个数组元素中存储多个对象吗?如果可以的话,最佳方法是什么?

英文:

Is it possible to store multiple objects in a single array element and if so, what would be the best method to go about doing that?

答案1

得分: 1

不。数组中的任何给定元素都只是一个元素。元素的类型确实很重要。

考虑一个整数数组:

int[] x = {1, 4, 7, 10};

一个元素不能“容纳”多个元素。

但现在考虑一个整数数组的数组:

int[][] x = {
    {1},
    {2, 3},
    {4, 5, 6},
    {7, 8, 9, 10}
};

每个元素仍然只是一个单独的元素。总共有四个类型为 int[] 的元素。然后,每个“那些”元素都有一个元素。理论上,你可以有任意多层这样的结构。这可能会很快变得混乱。

另一种在“一个元素中容纳多个元素”的方法是编写自己的类,以最有用的方式保存多个元素,并创建一个该类的数组。


注意:上面我使用了原始数组进行演示。然而,这也适用于 Java 的 List 对象:

ArrayList<Integer> x;  // 整数列表

ArrayList<ArrayList<Integer>> x;  // ArrayList<Integer> 对象的列表,每个对象本身都是整数列表

ArrayList<Integer>[] x;  // ArrayList<Integer> 对象的原始数组
                    // (我建议不要这样做,会变得混乱)

如果你的问题是关于将多种类型的对象放入单个元素中,那么你必须找出哪个超类涵盖了这两种类型(通常是 Object,如果类型差异较大),并使列表成为那种类型:

Object[] x = {Integer.valueOf(5), Double.valueOf(3.14), "some_string"};

将这些对象正确取出并放入它们的正确类型中是困难的,所以我不建议这样做。

你还可以尝试构建自己的 Union 类型,并使列表成为该类型。

英文:

No. Any given element of an array is just that, one element. The type of element does matter, though.

Consider an array of integers:

int[] x = {1, 4, 7, 10};

One element cannot 'hold' multiple elements.

But now consider an array of arrays of integers:

int[][] x = {
    {1},
    {2, 3},
    {4, 5, 6},
    {7, 8, 9, 10}
};

Each element is still just a single element. There are four elements each of type int[]. Then, each of those has one element per element. You can theoretically have as many layers of this as you want. It can get confusing very quickly.

Another way to fit 'multiple elements into one element' would be to write your own class that holds multiple elements in whatever way is most useful to you, and make an array of those.


Note: above, I've used primitive arrays to demonstrate. However, this also works with Java List objects:

ArrayList&lt;Integer&gt; x;  // list of integers

ArrayList&lt;ArrayList&lt;Integer&gt;&gt; x;  // list of ArrayList&lt;Integer&gt; objects, each of which is itself a list of integers

ArrayList&lt;Integer&gt;[] x;  // primitive array of ArrayList&lt;Integer&gt; objects
                         // (I recommend not doing this, it can get confusing)

If your question is about putting multiple types of objects in a single element, then you have to figure out what superclass encompasses both types (usually Object, if the types are remotely different), and make your list that type:

Object[] x = {Integer.valueOf(5), Double.valueOf(3.14), &quot;some_string&quot;};

Getting those objects back out again and putting them into their correct type, is difficult, so I don't recommend this.

You can also try building your own Union type, and making the list be that type.

答案2

得分: 0

你可以将任何内容存储在 Object[] 对象数组中。

就像这样:

Object[] objArr = new Object[10];
objArr[0] = "String";
objArr[1] = Arrays.asList(1, 2, 4);
objArr[2] = new ArrayList<>();
objArr[3] = 1;
英文:

You can store anything in an Object[] object array.

Something like this:

Object[] objArr = new Object[10];
objArr[0] = &quot;String&quot;;
objArr[1] = Arrays.asList(1, 2, 4);
objArr[2] = new ArrayList&lt;&gt;(); 
objArr[3] = 1;

huangapple
  • 本文由 发表于 2020年9月18日 02:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定