java 关于求ArrayList的最大元素的程序有错误,怎么解决?

「已注销」 2019-04-27 09:22:08
编写以下方法,返回一个整数ArrayList的最大值。如果列表为null或者列表的大小为0,则返回null值。 public static Integer max(ArrayList<Integer> list) 编写一个测试程序,提示用户输入一个以0结尾的数值序列,调用该方法返回输入的最大值。 package test11; import java.util.*; public class max { public static void MAX(ArrayList<Integer> list) { ArrayList<String> list1=new ArrayList<String>(); java.util.Collections.sort(list1); System.out.println(list1); String MAX=Collections.max(list1); System.out.println("max="+MAX);} public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<>(); Scanner input=new Scanner(System.in); System.out.println("Enter integers(input ends with 0)"); int value; do { value=input.nextInt(); } while(value!=0); MAX(list); } }
...全文
194 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
戎码一生灬 2019-08-07
  • 打赏
  • 举报
回复
List<Integer> ints = Arrays.asList(1,2213,4,54);
Integer max = Collections.min(ints);
System.out.println("max value is : " + max);

浮六 2019-08-07
  • 打赏
  • 举报
回复
第八行和第九行的list是排过序的吗
农夫丶果园 2019-04-28
  • 打赏
  • 举报
回复
java8 的话还可以这样写
List<Integer> ints = Arrays.asList(1,2213,4,54);
		Integer max = ints.stream().reduce(0, Integer::max);
		System.out.println("max value is : " + max);
哦呼 2019-04-27
  • 打赏
  • 举报
回复
在MAX方法里面加如
if(list.size == 0 || list = null){
System.out.println(null);
}
「已注销」 2019-04-27
  • 打赏
  • 举报
回复
引用 2 楼 哦呼的回复:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Max {
public static void MAX(ArrayList<Integer> list) {
Collections.sort(list);
System.out.println(list);
System.out.println("max=" + list.get(list.size() - 1));
}

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Enter integers(input ends with 0)");
int value;
do {
value = input.nextInt();
list.add(value);
} while (value != 0);
MAX(list);
}
}
谢谢 列表为null或者列表数值大小为0返回null值怎么写?
哦呼 2019-04-27
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Max {
public static void MAX(ArrayList<Integer> list) {
Collections.sort(list);
System.out.println(list);
System.out.println("max=" + list.get(list.size() - 1));
}

public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Enter integers(input ends with 0)");
int value;
do {
value = input.nextInt();
list.add(value);
} while (value != 0);
MAX(list);
}
}
哦呼 2019-04-27
  • 打赏
  • 举报
回复
引用 楼主 gty_9821 的回复:
编写以下方法,返回一个整数ArrayList的最大值。如果列表为null或者列表的大小为0,则返回null值。
public static Integer max(ArrayList<Integer> list)
编写一个测试程序,提示用户输入一个以0结尾的数值序列,调用该方法返回输入的最大值。
package test11;
import java.util.*;
public class max {
public static void MAX(ArrayList<Integer> list)
{ ArrayList<String> list1=new ArrayList<String>(); java.util.Collections.sort(list1); System.out.println(list1);
String MAX=Collections.max(list1); System.out.println("max="+MAX);}
public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<>();
Scanner input=new Scanner(System.in); System.out.println("Enter integers(input ends with 0)");
int value;
do {
value=input.nextInt(); }
while(value!=0);
MAX(list); } }

你这代码写的。。。我都无语了,好多没用的,而且你用一个新集合干什么??集合都是空的不报错就怪了
我贴代码了

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧