Java 迭代ArrayList以获得5个最大的数字

Java 迭代ArrayList以获得5个最大的数字,java,Java,是的,这是家庭作业,但我需要一些帮助。我已经能够通过最高的数字进行排序,但之后没有一个数字是正确的。编号清单: 现在,我们正在学习阵列,那么这仅仅是试图用炮弹射击苍蝇吗 package hw2; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class HW2 { public static Ar

是的,这是家庭作业,但我需要一些帮助。我已经能够通过最高的数字进行排序,但之后没有一个数字是正确的。编号清单: 现在,我们正在学习阵列,那么这仅仅是试图用炮弹射击苍蝇吗

    package hw2;

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;

    public class HW2 {

        public static ArrayList<Integer> nums1 = new ArrayList<Integer>();
        public static int size = 0;

        public static void main(String[] args) throws Exception {
            ArrayList<Integer> sortedNums = new ArrayList<Integer>();


            readFile();
            System.out.println("Name: Jay Bhagat" + "\n" + "Email: xxxxxx");
            size = nums1.size();

            for(int l = 0; l<=10;l++){
            nums1.set(sortThis(nums1, l), 90009);
            System.out.println("\n\n");
            }


    //        for (int k = 0; k <= size - 1; k++) {
    //            System.out.println("Number " + (k + 1) + sortedNums.get(k));
    //
    //        }


        }

        public static void readFile() throws Exception {
            BufferedReader reader = new BufferedReader(new FileReader("L:\\numbers.txt"));

            while (reader.readLine() != null) {
                nums1.add(Integer.parseInt((reader.readLine())));
            }

            reader.close();


        }

        public static int sortThis(ArrayList<Integer> current, int offset) {
            int j = 0;
            int tempNum = 0;
            int curNum = 0;
            int finalIndex = 0;
            int prevIndex = 0;
            int curIndex = 0;

            for (j = 0; j < size-offset; j++) {
                curIndex = j;
                nums1.trimToSize();
                curNum = current.get(j);
                //Thread.sleep(1000);
                if(curNum!=90009){
                if (curNum > tempNum) {
                    tempNum = curNum;
                    System.out.println(tempNum);
                    prevIndex = j;
                    finalIndex = prevIndex;
                }
                if (curNum < tempNum) {
                    finalIndex = prevIndex;
                }
                }    



            }
            return finalIndex;
        }
    }
包装hw2;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.util.ArrayList;
公共级HW2{
public static ArrayList nums1=新的ArrayList();
公共静态int size=0;
公共静态void main(字符串[]args)引发异常{
ArrayList sortedNums=新的ArrayList();
readFile();
System.out.println(“名称:Jay Bhagat”+“\n”+“电子邮件:xxxxxx”);
size=nums1.size();

对于(int l=0;l为什么不使用Collections.sort(List List)或Arrays.sort(arr)。这将节省大量工作。或者这是您任务的一部分?

假设您的集合已排序,并且您需要最后5个元素,请尝试以下方法:

for (int i = sortedNums.size() - 5; i < sortedNums.size(); ++i) {
  System.err.println(sortedNums.get(i));
}
for(int i=sortedNums.size()-5;i
您只需要两行代码:

Collections.sort(nums1);
List<Integer> high5 = nums1.subList(nums1.size() - 5, nums1.size());
Collections.sort(nums1);
List high5=nums1.subList(nums1.size()-5,nums1.size());
如果您必须“自己动手”,最简单的排序方法是:

  • 反复浏览列表
  • 如果相邻号码的顺序错误,则交换它们
  • 重复n次

效率不高,但很容易编码。

我将如何进行此操作:

创建临时ArrayList,作为初始ArrayList的副本。 找到每个最大的元素后,将其从临时ArrayList中删除,并将其添加到5个最大的数字中

重复,直到完成


编辑*这不需要对元素进行排序,因此效率相当低

这种方法只需对列表进行一次遍历,不需要排序:

声明一个由5个整数组成的数组:
int[]最大值=新的int[5];

数组列表中的前5个元素放入
最大的

从第6个元素开始,查看
数组列表中的每个元素N,如果N大于
最大的
中的任何元素,则抛出
最大的
中当前最小的数字,并将其替换为N


如果需要排除重复项,可以轻松修改算法(只需跳过
最大的
中已有的任何
数组列表
元素)。

考虑到这是一个家庭作业,我假设您没有使用
排序
之类的自由。下面是一个可以尝试实现的算法的概要

 create an array of five integers (we will keep this sorted)
 for each element in the list
   find the index of the element in the array that it is greater than
   if no such element exists in the array (i.e. it is smaller than all elements in the array)
     continue on to the next element in the list
   else
     push all elements in the array to one index below, letting them fall off the 
     edge if need be (e.g. if the number in list is 42 and the array has 
     45 and 40 at index 3 and 2 respectively then 
     move arr[1] to arr[0], and arr[2] (40) to arr[1] and set arr[2] = 42)
   end if
 end for
最后,数组将包含五个元素


我会留下一个问题让你回答(这很重要):您应该首先将数组设置为什么?

在对数组/列表进行排序后,为什么不只获取最后5个元素?这实际上是一个很好的问题,在面试中需要询问。显然,没有排序部分…25%的积分用于询问列表中是否有重复的数字。重复:因为这样做是O(n)(在数组中线性搜索5次),排序最多为O(n log n),如果列表足够大,则在某些情况下,除了“删除”之外,您的方法将比先排序更有效部分原因是,从
ArrayList
中删除可以移动很多元素。改进方法是使用
LinkedList
以使删除时间保持不变,或“删除”通过将其更改为Integer.MIN_值来创建一个元素,这样就不需要进行移位。虽然我确信集合是对数字进行排序的一种好方法,但我不确定我的教授是否会喜欢我自己不理解的代码。我只想告诉你:现实世界中没有人编写过自己的排序算法。每个人都使用集合ns.sort(),可能在需要自定义顺序时使用自定义比较器。学习JDK API就是学习语言。事实上,这同样适用于每种语言。没错,但这真的是编程教授的错误问题吗?我必须这样做,否则我的作业会得F。尽管我完全同意我永远不会重新发明轮子如果我不需要的话。好吧,在适应之后,我决定只使用bubblesort。它效率很低,当然,但它完成了任务,我只有1000个元素。谢谢!它将是:List high5=nums1.subList(0,5);因为列表已经排序。是的,排序是任务的一部分。