在使用数组列表和添加值方面遇到了一些问题。Java

huangapple go评论79阅读模式
英文:

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&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(n);
		Scanner input = new Scanner(System.in);

		System.out.println(&quot;Please enter 5 numbers: &quot;);
		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&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(n);
Scanner input = new Scanner(System.in);

System.out.println(&quot;Please enter 5 numbers: &quot;);
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

huangapple
  • 本文由 发表于 2020年4月8日 07:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定