gpt4 book ai didi

java - 返回最大尺寸的列表对象

转载 作者:行者123 更新时间:2023-12-01 22:33:08 25 4
gpt4 key购买 nike

如何返回长度最大的对象?

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer>list = new ArrayList<Integer>();
List<Integer>list1 = new ArrayList<Integer>();
HashSet<List<Integer>> hash = new HashSet<List<Integer>>();

list.add(2);
list1.add(3);
list1.add(4);
hash.add(list);
hash.add(list1);
}

public static List<Integer> meth(HashSet<List<Integer>> hash){
List<Integer> list = new ArrayList<Integer>();

if(hash.isEmpty()){
return list;
}
else{
for(List<Integer> value : hash){
// probably something here?
}
}
return list;
}

我知道 list1 比 list 大,但是我如何让 java 做到这一点?我希望它返回 List<Integer> 中最大的列表.

最佳答案

public static List<Integer> meth(HashSet<List<Integer>> hash){
List<Integer> list = new ArrayList<Integer>();

for(List<Integer> value : hash){
if(value.size() > list.size()) {
list = value;
}
}
return list;
}
  • 请注意,您可以省略 if-else 结构。
  • 此解决方案无法区分哈希集中最长列表的长度是否为零,或者哈希集是否为空。

关于java - 返回最大尺寸的列表<integer>对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com