【问题标题】:errors in java - finding maximum values in an ArrayListjava中的错误-在ArrayList中查找最大值
【发布时间】:2012-05-02 19:58:24
【问题描述】:

以下是我为回答问题而编写的程序-

“现在使用 ArrayList 和 Integer 包装类来存储值并通过使用 Scanner 类从控制台读取输入来初始化元素。扩展程序以识别 ArrayList 中的 n 个最大值。”

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

public class ArraylistInput {
/**
 * @param args
 */
public static void main(String[] args) {

    ArrayList<Integer> val = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the length of you Array List ");
    int nos = in.nextInt();
    // Recorrd the input numbers
    for (int i = 0 ; i < nos; i++)
    {
        System.out.println("Enter values for the ArrayList ");
        int Input = in.nextInt();
        val.add(Input);
    }

    // Display the arraylist
    for (int j = 0; j < nos; j++)
    {
        int x = val.get(j);
        System.out.println("Index " + (j+1) + ": " +  x);
    }
    System.out.println("How meny maximmum values do you want? ");
    int max =0; // initial max value
    int nmax = in.nextInt(); // number of maximum values
    int length = val.size(); // size of the arraylist

    // finding the maximum values in ascending order without sorting
    for (int h = 1; h <= nmax ; h++)
    {           
        for (int k=0;k < length; k++)
        {
            if (val.get (k) > max)
            {
                 max = val.get(k);
            }               
        }
        System.out.println ("maximmum = " + max);
        int z = val.indexOf(max); // removing the higest value after printing
        val.remove(z);
    }           
    }
}

输出和错误:

输入数组列表的长度

3

输入 ArrayList 的值

12

输入 ArrayList 的值

45

输入 ArrayList 的值

8

索引 1:12 索引 2:45 索引 3:8

您想要多少个最大值?

2

最大值 = 45

线程“主”中的异常最大值 = 45 java.lang.ArrayIndexOutOfBoundsException:-1 在 java.util.ArrayList.elementData(未知来源)在 java.util.ArrayList.remove(未知来源)在 ArraylistInput.main(ArraylistInput.java:46)

【问题讨论】:

    标签: java if-statement for-loop arraylist


    【解决方案1】:

    我会做以下事情:

    Collections.sort(myList, Collections.reverseOrder());
    List<Integer> maxn = myList.subList(0, n);
    System.out.printf("The maximum %d values are: %s%n", n, maxn);
    maxn.clear(); //This clears the sublist and removes its elements from the source list
    

    这将为您提供一个列表,其中包含列表中最多 n 个元素。

    【讨论】:

    • 我也更喜欢这个。但请记住,subList() 返回一个视图。对myList 的任何更改都将反映在maxn 中。
    • @nansen 然后将子列表添加到另一个列表中:List&lt;Integer&gt; maxn=new ArrayList&lt;&gt;(myInts.subList(0,n))
    【解决方案2】:

    你只有一个 ArrayList 你不需要嵌套的 for 循环来找到最大值:

    int max = Integer.MIN_VALUE;
    
    for(int i = 0; i < list.size(); i++)
    {
       current = list.get(i);
       if(current > max)
          max = current;
    } 
    

    当您搜索最大值时,嵌套的 for 循环会尝试访问列表中不存在的值,这就是您收到此错误的原因。

    【讨论】:

    • 我相信问题在于获取n 最大元素,而不仅仅是一个。
    • 很容易调整它以获得最大 n
    【解决方案3】:

    你的max 永远不会被分配,然后你试图从数组列表中删除一个不存在的元素。将max 设置为列表中不能出现的某个值,然后检查它是否曾在循环中分配。

    【讨论】:

      【解决方案4】:

      当你从列表中删除一个元素时:

      val.remove(z);

      您更改了列表的大小,但没有更新您的 length 变量。这会导致您尝试访问超出数组大小的索引,从而导致java.lang.ArrayIndexOutOfBoundsException

      另外,考虑保存最大值的索引最大值本身。然后,当您删除该值时,您可以直接使用ArrayList.remove(),而无需再次搜索整个列表以获得最大索引(这是ArrayList.indexOf() 将做的)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-07
        • 2015-07-21
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多