简体   繁体   English

Java:如何创建一个接受第一个元素列表的列表 cons

[英]Java: how to create a list cons that accepts lists for the first element

When I create a Java list and I want that the head is a list, should it print a [[-1,0],1,2,3,4] or its alright it just leave the sublist [-1, 0] as two separated elements like [-1, 0, 1, 2,3,4] and, how can I get the first structure as an aswer.当我创建一个 Java 列表并且我希望头部是一个列表时,它应该打印一个 [[-1,0],1,2,3,4] 还是没关系,它只是离开子列表 [-1, 0]作为两个分离的元素,如 [-1, 0, 1, 2,3,4] ,我怎样才能得到第一个结构作为 aswer。

From your post I guess you are asking about how to create a list "construct" in Java which contains both list and non-list elements.从您的帖子中,我猜您是在询问如何在 Java 中创建一个列表“构造”,其中包含列表和非列表元素。

In Java all elements of your list/array need to have the same type.在 Java 中,列表/数组的所有元素都需要具有相同的类型。 In the example you posted ( [[-1,0],1,2,3,4] ), the elements are not all the same type.在您发布的示例中( [[-1,0],1,2,3,4] ),元素的类型并不完全相同。 What you have is this: [List<Integer>, Integer, Integer, Integer, Integer] .您所拥有的是: [List<Integer>, Integer, Integer, Integer, Integer] The first one is different, which isn't allowed in Java.第一个不同,在Java中是不允许的。

So, what you need to do is make it so that all elements of your list/array are the same type.所以,你需要做的是让你的列表/数组的所有元素都是相同的类型。

For your case, where there are only integers in your list , the easiest thing to do is to change your types to this: [List<Integer>, List<Integer>, List<Integer>, List<Integer>, List<Integer>] (with your specific values: [[-1,0],[1],[2],[3],[4]] ).对于您的情况,您的 list 中只有整数,最简单的方法是将您的类型更改为: [List<Integer>, List<Integer>, List<Integer>, List<Integer>, List<Integer>] (使用您的特定值: [[-1,0],[1],[2],[3],[4]] )。 Instead of a list of List<Integer> and Integer , you now have a list containing only List<Integer> (some of your lists happen to contain only 1 element, but that is ok).而不是List<Integer>Integer的列表,您现在有一个仅包含List<Integer>的列表(您的某些列表恰好只包含 1 个元素,但这没关系)。

This is called a "two-dimensional list" and depending on whether you want to code it with arrays or java.util.List s you can code it in one of the following ways:这称为“二维列表”,根据您是否要使用 arrays 或java.util.List进行编码,您可以通过以下方式之一对其进行编码:

// with arrays

int[] myTwoDarr = int[5][];
myTwoDarr[0] = new int[]{-1, 0};
myTwoDarr[1] = new int[]{1};
myTwoDarr[2] = new int[]{2};
myTwoDarr[3] = new int[]{3};
myTwoDarr[4] = new int[]{4};

// With Lists

List<List<Integer>> myList = new LinkedList<>();
List<Integer> nestedList1 = new LinkedList<>();
nestedList.add(-1);
nestedList.add(0);
myList.add(nestedList1);
List<Integer> nestedList2 = new LinkedList<>();
nestedList2.add(1);
myList.add(nestedList2);
...etc...

Now, if you absolutely must have different kinds of data in your list, you can create a list of Object instead.现在,如果您的列表中绝对必须有不同类型的数据,您可以创建一个Object列表。

In Java Object is a type from which all non-primitive types inherit.在 Java 中, Object是所有非原始类型都继承自的类型。 Thus, as long as something is not a primitive, it is an Object and can go in a list of Object .因此,只要某些东西不是原语,它就是Object并且可以在 Object 列表中的Object中。 This will allow you to put any kind of data in the elements of your list.这将允许您将任何类型的数据放入列表的元素中。 Using Object allows you to have a list of all the same type which looks like this: [Object, Object, Object, Object, Object] . Using Object allows you to have a list of all the same type which looks like this: [Object, Object, Object, Object, Object] . But, now Java does not know the specific types you actually have stored in your elements so you will need to type cast when getting them.但是,现在 Java 不知道您实际存储在元素中的特定类型,因此您需要在获取它们时进行类型转换。

Here's an example using a java.util.List :这是使用java.util.List的示例:

List<Object> myList = new LinkedList<>();
List<Integer> nestedList = new LinkedList<>();
nestedList.add(-1);
nestedList.add(0);
myList.add(nestedList);
myList.add(1);
myList.add("a string");

// You need to cast when taking elements out of the list.
List<Integer> newNest = (List<Integer>) myList.get(0);
Integer myInt = (Integer) myList.get(1);
String myString = (String) myList.get(2);

Since all your base data types are the same ( Integer / int ) I recommend avoiding the Object list method and going with the 2D list.由于所有基本数据类型都相同( Integer / int ),我建议避免使用Object列表方法并使用 2D 列表。 It's simpler and less error prone to not do all this casting.不做所有这些转换更简单,更不容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM