Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 合并排序进入无限循环?_Java_Arraylist_Mergesort - Fatal编程技术网

Java 合并排序进入无限循环?

Java 合并排序进入无限循环?,java,arraylist,mergesort,Java,Arraylist,Mergesort,作为我正在进行的项目的一部分,我们需要获取一个“Song”对象,其列表位于ArrayList“songCollection”中。我需要根据标题、等级、艺术家或年份对文件进行合并排序,但执行时,代码会进入无限循环(StackOverflow错误),我不确定原因。我的代码有什么问题: 歌曲列表: 1965 55加里·刘易斯和花花公子每个人都喜欢小丑 1963 526本·E·金我怎么能忘记 1979年207阿什福德和辛普森找到了治疗方法1981年96安迪·吉布时间是1996年169男孩和女孩见面的总时

作为我正在进行的项目的一部分,我们需要获取一个“Song”对象,其列表位于ArrayList“songCollection”中。我需要根据标题、等级、艺术家或年份对文件进行合并排序,但执行时,代码会进入无限循环(StackOverflow错误),我不确定原因。我的代码有什么问题: 歌曲列表: 1965 55加里·刘易斯和花花公子每个人都喜欢小丑 1963 526本·E·金我怎么能忘记 1979年207阿什福德和辛普森找到了治疗方法1981年96安迪·吉布时间是1996年169男孩和女孩见面的总时间 1962 384琳达·斯科特·耶西雷 Frank Sinatra你对我的爱 1978 302约翰·丹佛我想活下去 1954年304斯坦·肯顿和他的管弦乐队 1993年,199年,只要我能梦想 1989 269上议院我想被爱 1950 267本尼·古德曼六重奏哦,宝贝! 1948 295特克斯·威廉姆斯与西部商队

代码: //要将上面的一行更改为歌曲,请将其添加到songCollection

 ArrayList<Song> songCollection = new ArrayList<Song>();
 public SongCollection(String fileName) throws FileNotFoundException {
      Scanner input = new Scanner(new File(fileName));
      while (input.hasNextLine())
      {
         addSong(input.nextLine());

      }
}
//getter method
public ArrayList<Song> getSongCollection() {
    return songCollection;
}
public void addSong(String line) {
    //parse the line read from the song file, create the song, and add it to the collection
    StringTokenizer token = new StringTokenizer(line, "\t");
    String year = token.nextToken();
    int intYear = Integer.parseInt(year);

    String rank = token.nextToken();
    int intRank = Integer.parseInt(rank);

    String artist = token.nextToken();
    String name = token.nextToken();

    Song song = new Song(intYear, intRank, artist, name);
    songCollection.add(song);




}


                 public ArrayList<Song>  mergeSort(ArrayList<Song> all, String filter, String file) throws FileNotFoundException {
     //overarching method, divides the array list into left and right array lists, then divides and sorts, and then finally merges back
     //distinction: use of mergeString vs mergeInt based on the type of filter that the list is being sorted for. 
     ArrayList<Song> left = new ArrayList<Song>();
        ArrayList<Song> right = new ArrayList<Song>();
        int center;

        if (all.size() == 1) {    
            return all;
        } 
        else {
            center = all.size()/2;
            for (int i=center; i<all.size(); i++) {
                    right.add(all.get(i));
            }


            //left  = mergeSort(left,filter, file);
            //right = mergeSort(right, filter, file);

            // Merge the results back together.
            if (filter.equalsIgnoreCase("year") || filter.equalsIgnoreCase("rank"))
                mergeInt(left, right, all, filter);
            if (filter.equalsIgnoreCase("artist") || filter.equalsIgnoreCase("title"))
                mergeString(left,right,all,filter);

        }
        PrintStream out = new PrintStream(new File(file));
        for (int x = 0; x < songCollection.size(); x++) {
            out.println(all.get(x).toString());
        }
        out.close();
        return all;

    }
            private void mergeString(ArrayList<Song> left, ArrayList<Song> right, ArrayList<Song> whole, String filter) {
        //'mergeX' for the string filters
        int leftIndex = 0;
        int rightIndex = 0;
        int wholeIndex = 0;

        //checks whether filter is artist or type 
        if(filter.equalsIgnoreCase("artist")) {
            while (leftIndex < left.size() && rightIndex < right.size()) {
                if ( (left.get(leftIndex).artist.compareTo(right.get(rightIndex).artist)) < 0) {
                    whole.set(wholeIndex, left.get(leftIndex));
                    leftIndex++;
                } else {
                    whole.set(wholeIndex, right.get(rightIndex));
                    rightIndex++;
                }
                wholeIndex++;
            }

            ArrayList<Song> rest;
            int restIndex;
            if (leftIndex >= left.size()) {
                // The left ArrayList has been use up...
                rest = right;
                restIndex = rightIndex;
            } else {
                rest = left;
                restIndex = leftIndex;
            }

            for (int i=restIndex; i<rest.size(); i++) {
                whole.set(wholeIndex, rest.get(i));
                wholeIndex++;
            }
        }
        if(filter.equalsIgnoreCase("title")) {
            while (leftIndex < left.size() && rightIndex < right.size()) {
                if ( (left.get(leftIndex).title.compareTo(right.get(rightIndex).title)) < 0) {
                    whole.set(wholeIndex, left.get(leftIndex));
                    leftIndex++;
                } else {
                    whole.set(wholeIndex, right.get(rightIndex));
                    rightIndex++;
                }
                wholeIndex++;
            }

            ArrayList<Song> rest;
            int restIndex;
            if (leftIndex >= left.size()) {
                rest = right;
                restIndex = rightIndex;
            } else {
                rest = left;
                restIndex = leftIndex;
            }

            for (int i=restIndex; i<rest.size(); i++) {
                whole.set(wholeIndex, rest.get(i));
                wholeIndex++;
            }
        }
    }

    private void mergeInt(ArrayList<Song> left, ArrayList<Song> right, ArrayList<Song> whole, String filter) {
        //same as mergeString, but for int fields
        int leftIndex = 0;
        int rightIndex = 0;
        int wholeIndex = 0;

        if(filter.equalsIgnoreCase("year")) {
            while (leftIndex < left.size() && rightIndex < right.size()) {
                if  (left.get(leftIndex).year < right.get(rightIndex).year) {
                    whole.set(wholeIndex, left.get(leftIndex));
                    leftIndex++;
                } else {
                    whole.set(wholeIndex, right.get(rightIndex));
                    rightIndex++;
                }
                wholeIndex++;
            }

            ArrayList<Song> rest;
            int restIndex;
            if (leftIndex >= left.size()) {
                rest = right;
                restIndex = rightIndex;
            } else {
                rest = left;
                restIndex = leftIndex;
            }

            for (int i=restIndex; i<rest.size(); i++) {
                whole.set(wholeIndex, rest.get(i));
                wholeIndex++;
            }
        }
        if(filter.equalsIgnoreCase("rank")) {
            while (leftIndex < left.size() && rightIndex < right.size()) {
                if  (left.get(leftIndex).rank < (right.get(rightIndex).rank)) {
                    whole.set(wholeIndex, left.get(leftIndex));
                    leftIndex++;
                } else {
                    whole.set(wholeIndex, right.get(rightIndex));
                    rightIndex++;
                }
                wholeIndex++;
            }

            ArrayList<Song> rest;
            int restIndex;
            if (leftIndex >= left.size()) {
                rest = right;
                restIndex = rightIndex;
            } else {
                rest = left;
                restIndex = leftIndex;
            }
            for (int i=restIndex; i<rest.size(); i++) {
                whole.set(wholeIndex, rest.get(i));
                wholeIndex++;
            }
ArrayList songCollection=new ArrayList();
公共歌曲集合(字符串文件名)引发FileNotFoundException{
扫描仪输入=新扫描仪(新文件(文件名));
while(input.hasNextLine())
{
addSong(input.nextLine());
}
}
//吸气剂法
公共ArrayList getSongCollection(){
回收歌曲;
}
公共void addSong(字符串行){
//解析从歌曲文件中读取的行,创建歌曲,并将其添加到集合中
StringTokenizer令牌=新的StringTokenizer(行“\t”);
字符串year=token.nextToken();
int intYear=Integer.parseInt(年);
String rank=token.nextToken();
intintrantk=Integer.parseInt(秩);
字符串艺术家=token.nextToken();
字符串名称=token.nextToken();
歌曲=新歌(intYear、intRank、艺术家、姓名);
歌曲集。添加(歌曲);
}
公共ArrayList mergeSort(ArrayList all、字符串筛选器、字符串文件)引发FileNotFoundException{
//方法,将数组列表划分为左数组列表和右数组列表,然后进行分割和排序,最后合并回来
//区别:根据列表排序的过滤器类型,使用mergeString vs mergeInt。
ArrayList left=新的ArrayList();
ArrayList right=新的ArrayList();
国际中心;
如果(all.size()==1){
全部归还;
} 
否则{
中心=所有.size()/2;
for(int i=center;i=left.size()){
//左数组列表已用完。。。
休息=右;
restIndex=rightIndex;
}否则{
休息=左;
restIndex=leftIndex;
}
for(int i=restIndex;i=left.size()){
休息=右;
restIndex=rightIndex;
}否则{
休息=左;
restIndex=leftIndex;
}
for(int i=restIndex;i=left.size()){
休息=右;
restIndex=rightIndex;
}否则{
休息=左;
restIndex=leftIndex;
}
for(int i=restIndex;i=left.size()){
休息=右;
restIndex=rightIndex;
}否则{
休息=左;
restIndex=leftIndex;
}

对于(int i=restIndex;这听起来是学习使用调试器的绝佳机会。您能发布您收到的错误消息吗?我的错误消息是:“线程中的异常”main“java.lang.StackOverflowerr at Song.SongCollection.mergeSort(SongCollection.java:270)”,第270行是left=mergeSort(left,filter,file);您能添加一个可编译的示例吗?这样我们就可以进行测试了?使用java的集合对其进行排序……无需重新发明轮子