英文:
Having some problems with Array Lists and adding values. Java
问题
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
public class randomArrayList{
public static void main(String[] args){
int n = 5;
ArrayList<Integer> list = new ArrayList<Integer>(n);
Scanner input = new Scanner(System.in);
System.out.println("Please enter 5 numbers: ");
int num1 = input.nextInt();
list.add(num1);
int num2 = input.nextInt();
list.add(num2);
int num3 = input.nextInt();
list.add(num3);
int num4 = input.nextInt();
list.add(num5);
int num5 = input.nextInt();
list.add(num5);
Arrays.sort(list);
System.out.println(list);
}
}
英文:
I was working on this problem using arrays and array lists, and then I want to sort the array list to that the numbers the user enters are in ascending over. When I try to run this, I get two error codes, 1: the period on the list.add(num1); is not recognized, and then the other error code is that the Arrays.sort(list); is not recognized, I honestly have no clue what to do, I searched the web all over and could not find anything, this is the sole reason I made this account xD. Please help!!!
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;
public class randomArrayList{
public static void main(String[] args){
int n = 5;
ArrayList<Integer> list = new ArrayList<Integer>(n);
Scanner input = new Scanner(System.in);
System.out.println("Please enter 5 numbers: ");
int num1 = input.nextInt();
list.add(num1);
int num2 = input.nextInt();
list.add(num2);
int num3 = input.nextInt();
list.add(num3);
int num4 = input.nextInt();
list.add(num5);
int num5 = input.nextInt();
list.add(num5);
Arrays.sort(list);
System.out.println(list);
}
}
答案1
得分: 1
这就是你想要的
注意
在声明变量之前不能添加 list.add(num5);
。我认为你想要添加 num4
还有
Arrays.sort(list);
顾名思义是用于对数组进行排序。
使用 Collections.sort(list)
替代
int n = 5;
ArrayList<Integer> list = new ArrayList<Integer>(n);
Scanner input = new Scanner(System.in);
System.out.println("请输入 5 个数字:");
int num1 = input.nextInt();
list.add(num1);
int num2 = input.nextInt();
list.add(num2);
int num3 = input.nextInt();
list.add(num3);
int num4 = input.nextInt();
list.add(num4);
int num5 = input.nextInt();
list.add(num5);
Collections.sort(list);
System.out.println(list);
尽管更清晰的方式是使用循环
英文:
This is what you want
Note
can not add list.add(num5);
before you have declared the variable. I think you want to add num4
and
Arrays.sort(list);
as the name implies is for sorting Arrays.
use Collections.sort(list)
instead
int n = 5;
ArrayList<Integer> list = new ArrayList<Integer>(n);
Scanner input = new Scanner(System.in);
System.out.println("Please enter 5 numbers: ");
int num1 = input.nextInt();
list.add(num1);
int num2 = input.nextInt();
list.add(num2);
int num3 = input.nextInt();
list.add(num3);
int num4 = input.nextInt();
list.add(num4);
int num5 = input.nextInt();
list.add(num5);
Collections.sort(list);
System.out.println(list);
Although a cleaner way would to be use a loop
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论