Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以更清晰的方式生成列表?_Java_List - Fatal编程技术网

Java 如何以更清晰的方式生成列表?

Java 如何以更清晰的方式生成列表?,java,list,Java,List,我有以下元素列表(称之为lst),例如: [1, 2, 2, 5, 78, 768, 6823, 43, 234] 我需要创建列表其中 public class Container{ private int value; private List<Integer> children; public Container(int i){ //impl } //other staff } 代码看起来非常复杂。因此,我想请教您如何做得更好?您可以这样做

我有以下元素列表(称之为
lst
),例如:

[1, 2, 2, 5, 78, 768, 6823, 43, 234]
我需要创建
列表
其中

public class Container{

   private int value;
   private List<Integer> children;

   public Container(int i){ //impl
   }

   //other staff
}

代码看起来非常复杂。因此,我想请教您如何做得更好?

您可以这样做:

List<Integer> lst; //the list of the integers
lst.removeAll(Collections.singleton(Collections.min(lst)));

Collections.sort(lst);

List<Container> containers = new ArrayList<>();
//filling the containers according to the requirements;
列表lst//整数的列表
lst.removeAll(Collections.singleton(Collections.min(lst));
集合。排序(lst);
列表容器=新的ArrayList();
//按要求装填容器;

由于您只使用leasteem列表删除所有元素,这些元素等于leasteem,因此您只需从列表中删除所有leasteem,结果将是相同的。

我不理解定义容器及其子容器的逻辑。为什么
Container(43)
有一个孩子
Container(5)
?也许它看起来非常奇怪,因为您的需求非常奇怪?:-)@Kayaman,因为43的前一个是5。所以,对于列表中的每个数字,您希望创建一个容器,并且该容器应具有前一个数字作为子项,与列表中的子项数字相同的次数?这是规定吗?
List<Integer> lst; //the list of the integers
lst.removeAll(Collections.singleton(Collections.min(lst)));

Collections.sort(lst);

List<Container> containers = new ArrayList<>();
//filling the containers according to the requirements;