简体   繁体   English

如何在Java中对文本文件进行冒泡排序

[英]how to bubble sort through a text file in java

How do I properly bubble sort through a text file by assigning the values to an array. 如何通过将值分配给数组来对文本文件进行适当的冒泡排序。 In the code below I tried to assign the values from the text file to a string while there is still something to fetch. 在下面的代码中,我尝试将文本文件中的值分配给字符串,同时仍然需要提取某些内容。 Then I used a for loop to assign the one that I have fetch to the array. 然后,我使用了for循环将已获取的内容分配给数组。 Then tried to use the bubble sort to hopefully sort the numbers that I have fetch from highest to lowest. 然后尝试使用冒泡排序将希望获取的数字从最高到最低排序。 But I get this one as an output: 但是我得到这个作为输出:

5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  
10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  
4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  
20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  20  
100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  100  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  78  
12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  12  
29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  29  

Here's my code: 这是我的代码:

     try{
 int i;
 String ss;
   FileReader fr;
      fr = new FileReader (new File("X:\\file.txt"));
      BufferedReader br = new BufferedReader (fr);


while ((ss = br.readLine()) != null) {
    String[] sv = ss.split(" ");


        String splayer_name=sv[1];
        String s_player_score=sv[2];

 for(int xy=0;xy<player_name.length;xy++){
        player_name[xy]=splayer_name;
        player_score[xy]=Integer.parseInt(s_player_score);
}


bubble_srt(player_score, player_score.length);

    for(i = 0; i <player_score.length; i++)
      System.out.print(player_score[i]+"  ");
    System.out.println();




  }

  }catch(Exception e){}

Please help, 请帮忙,

update: someone asked about the file structure, are you referring to this one: This is the file.txt, the one that I'm fetching is the rightmost number: 更新:有人问文件结构,您指的是这个:这是file.txt,我要提取的是最右边的数字:

1 a 5
2 b 10
5 x 4
7 h 20

I think your main problem is with the loop: 我认为您的主要问题在于循环:

 for(int xy=0;xy<player_name.length;xy++){
        player_name[xy]=splayer_name;
        player_score[xy]=Integer.parseInt(s_player_score);
}

Every time you read in a line. 每次您读一行。 You are replacing the entire contents of the *player_name* array with the splayer_name, and the the entire contents of the *player_score* with the int value of s_player_score. 您要用splayer_name替换* player_name *数组的全部内容,并用s_player_score的int值替换* player_score *的全部内容。

I am guessing that what you are trying to do is add a single new entry to player_name and player-score. 我猜您正在尝试做的是在player_name和player-score中添加一个新条目。 The size of the two arrays have to be equal to the number of rows in the text file, so you might want to use some form of List rather than an array. 这两个数组的大小必须等于文本文件中的行数,因此您可能要使用某种形式的List而不是数组。

For both the storage arrays (or Lists) add the name once and the score once and I think that will get you well on the way to what you are trying to do. 对于两个存储阵列(或列表),一次添加一个名称,一次添加一个分数,我认为这将使您在尝试做的事情中获得成功。

  1. Move the for loop & the bubble_srt() call outside the while loop. 将for循环和bubble_srt()调用bubble_srt() while循环之外。 You are replacing the entire contents on each iteration like what the earlier answer has correctly pointed out. 您将在每次迭代中替换全部内容,就像前面的答案正确指出的那样。 You only want to sort AFTER the entire file contents are read. 您只希望在读取整个文件内容之后进行排序。
  2. Since your file can be of any size there is no way to initialize the array beforehand unless you want to perform two reads one to get the count and then to read into the array. 由于您的文件可以是任何大小,因此除非您要执行两次读取以获取计数然后读入数组,否则无法预先初始化数组。 You would be better off using a List. 使用List会更好。 Here's a skeleton code: 这是基本代码:

     try { int i; String ss; FileReader fr; fr = new FileReader(new File("file.txt")); BufferedReader br = new BufferedReader(fr); int xy=0; List<String> player_name = new ArrayList<String>(); List<Integer> player_score = new ArrayList<Integer>(); 
     while ((ss = br.readLine()) != null) { String[] sv = ss.split(" "); player_name.add(sv[1]); player_score.add(Integer.parseInt(sv[2])); } bubble_srt(player_score); 
    \n\n} catch (Exception e) { } catch(Exception e){\n} }\n

(Modify your bubble sort to take a list input) (修改您的气泡排序以接受列表输入)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM