Java ArrayList未正确添加

时间:2015-12-31 04:56:29

标签: java arraylist

我对我认为的逻辑有困难。我的结果没有正确加起来。有人可以帮忙吗?问题出在arrayList.sizenums.length或其他内容中。我输入50 40 60作为我的整数。

public class Application {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<Integer>();
        System.out.print("Enter integers please ");
        System.out.println("(EOF or non-integer to terminate): ");

        while (input.hasNextInt()) {
            list.add(input.nextInt());
        }

        Integer[] nums = list.toArray(new Integer[0]);
        System.out.printf("%s", "You entered: ");
        for (int i = 0; i < nums.length; i++) {         
            System.out.printf("%d%s", nums[i], ", ");
        }

        Collections.sort(list);
        int b = Collections.max(list);
        int c = Collections.min(list);
        int arraySize = nums.length-1;
        double sum = 0;

        for(int i = 0; i < list.size(); i++)
        {
            sum += list.get(i);
        }
        System.out.println(" \nLast Number is : " + list.get(arraySize)
                + "\nLargest Number is: " + b 
                + "\nSmallest number is :" + c 
                + "\n" + "You entered " + (arraySize+1) + " numbers"
                + "\nTotal numbers added up is: " + sum 
                + "\nAverage number is: " + (sum / (nums.length)));
        input.close();
    }
}

1 个答案:

答案 0 :(得分:2)

您正在排序列表,因此它将从排序列表中返回最后一个值。 Collections.sort(list)正在对asc进行排序,以便从列表中获取最大数量。第一种方法,

1)您必须在排序之前管理原始列表,如

 ArrayList<Integer> ori = new ArrayList<Integer>(list);
 Collections.sort(list);

从ori获得价值。

 System.out.println(" \nLast Number is : " + ori.get(list.size()-1)

2)第二种方法,创建变量,它将存储临时最后输入的值。你可以用它。 可能有帮助!!

所以你的例子就像:

public static void main(final String[] args) {
         Scanner input = new Scanner(System.in);
            ArrayList<Integer> list = new ArrayList<Integer>();
            System.out.print("Enter integers please ");
            System.out.println("(EOF or non-integer to terminate): ");

            while (input.hasNextInt()) {
                list.add(input.nextInt());
            }

            Integer[] nums = list.toArray(new Integer[0]);
            System.out.printf("%s", "You entered: ");
            for (int i = 0; i < nums.length; i++) {         
                System.out.printf("%d%s", nums[i], ", ");
            }
            ArrayList<Integer> ori = new ArrayList<Integer>(list);
            Collections.sort(list);
            int b = Collections.max(list);
            int c = Collections.min(list);
            int arraySize = nums.length-1;
            double sum = 0;

            for(int i = 0; i < list.size(); i++)
            {
                sum += list.get(i);
            }
            System.out.println(" \nLast Number is : " + ori.get(list.size()-1)
                    + "\nLargest Number is: " + b 
                    + "\nSmallest number is :" + c 
                    + "\n" + "You entered " + (arraySize+1) + " numbers"
                    + "\nTotal numbers added up is: " + sum 
                    + "\nAverage number is: " + (sum / (nums.length)));
            input.close();

    }