简单问题:我应该在我的方法参数中输入什么来输入我的数组列表?

我的自定义类中有一个方法可以对我的 arraylist 进行排序。唯一的问题是我不确定将我的 arraylist 输入到方法中的语法,错误只是出现说“类型的非法开始”。这似乎是一个非常简单的问题,我只是缺乏语法知识,而且似乎无法在互联网上找到任何向我展示的内容。任何帮助将不胜感激。


public ArrayList <Integer> selectionSort(<Integer> recordTimes){ //The selection sort algorithm


        for(int i=0;i<recordTimes.length-1;i++){  //Iterate through the list of numbers

            for(int j=i+1;j<recordTimes.length;j++){ 

                if(recordTimes[i]>recordTimes[j]){

                    int temp = recordTimes[i];

                    recordTimes[i] = recordTimes[j];

                    recordTimes[j] = temp;

                }

            }

        }

        return recordTimes;

    }

错误信息:“类型的非法开始”


白衣染霜花
浏览 174回答 3
3回答

明月笑刀无情

由于您使用recordTimes.length,recordTimes必须Integer[]是一个array,而不是ArrayList.任何一个:声明Integer[] recordTimes(并将返回类型Integer[]也更改为);将其声明为,并在交换操作中ArrayList<Integer> recordTimes使用size()而不是length, 和(尽管这是一种交换列表元素的更简洁的方法)。getsetCollections.swap

BIG阳

这是解决方案,但我没有检查您是否正确实施了选择排序。int [] array = {2,4,5,6,4,7,9,4};int [] sortedArray = selectionSort(array);public int[] selectionSort(int [] recordTimes){ //The selection sort algorithm&nbsp; &nbsp; for(int i=0;i<recordTimes.length-1;i++){&nbsp; //Iterate through the list of numbers&nbsp; &nbsp; &nbsp; &nbsp; for(int j=i+1;j<recordTimes.length;j++){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(recordTimes[i]>recordTimes[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = recordTimes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recordTimes[i] = recordTimes[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recordTimes[j] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return recordTimes;}

墨色风雨

您应该将整数的 ArrayList 传递给方法,例如public ArrayList<Integer> selectionSort(ArrayList<Integer> recordTimes) {&nbsp; // rest of your code.}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java