mergeSort进入无限循环?

时间:2018-06-05 04:25:11

标签: java arraylist mergesort

作为我正在研究的项目的一部分,我们需要一个“Song”对象,其列表在ArrayList“songCollection”中。我需要根据标题,排名,艺术家或年份合并排序文件,但执行时,代码进入无限循环(StackOverflow Error),我不知道为什么。我的代码出了什么问题: 歌曲列表: 1965年55加里刘易斯和花花公子每个人都喜欢小丑 1963 526 Ben E. King我怎么能忘记 1979年207阿什福德和辛普森发现一个治愈1981年96安迪吉布时间是时间1996年169总计男孩遇见女孩 1962年384 Linda Scott Yesiree 1957 315 Frank Sinatra你对我的爱 1978 302约翰丹佛我想活下去 1954年304 Stan Kenton&他的乐团The Creep 1993 199Expos�尽我所能 1989 269上议院我想被爱 1950 267 Benny Goodman六重奏哦,宝贝! 1948 295 Tex Williams& Western Caravan Suspicion

代码: //将上面一行更改为歌曲,将其添加到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++;
            }

0 个答案:

没有答案