【问题标题】:Bubble Sort numbers from file in javajava中对文件中的数字进行冒泡排序
【发布时间】:2016-05-01 16:52:23
【问题描述】:

我正在尝试对文本文件中的数字进行冒泡排序,我了解如何进行冒泡排序以及如何使用文本文件。但是从来没有同时使用过它们。我尝试对数组进行冒泡排序,只是想弄清楚如何用文本文件替换该数组。如果有人可以向我解释如何让冒泡排序读取文本文件,将不胜感激。我是 Java 新手,有时将我学到的 2 种不同的东西组合到 1 个程序中会让人感到困惑。

这是我解决数组的冒泡排序:

  public static void main(String[] args)
 {

  int number[]={7,13,4,5,62,3,1,3,45};

  int temp;
  boolean fixed=false;
  while(fixed==false){
      fixed = true;

  for (int i=0; i <number.length-1;i++){ 
      if (number[i]>number[i+1]){

      temp = number [i+1];

      number[i+1]=number[i];

      number[i]=temp;
      fixed=false;
      }
 }
}
  for (int i=0; i<number.length;i++){
      System.out.println(number[i]);
   }
  }

}

【问题讨论】:

  • 您提出了一个一般性问题,所以我将给出一个一般性答案。只需从文件中读取数字。将字符串解析为 int 数组(如果您想使用您编写的示例进行排序)然后将排序后的数组写回文件

标签: java file bubble-sort


【解决方案1】:

使用 Scanner 类!

File file=new File("file.txt");
Scanner sc=new Scanner(file);
int arr[]=new int[100];
int i=0;
while(sc.hasNextLine()){
   arr[i]=sc.nextInt();
   i++;
}

【讨论】:

    【解决方案2】:

    不要只对数组进行硬编码..!

    假设您的文件内容是一个数字列表,由一些分隔符分隔,例如单个空格“”

    用途:

    File file=new File("file.txt");
    Scanner sc=new Scanner(file);
    String arr[] = sc.nextLine().split(" ");
    

    就是这样。 一旦你得到了数组,你就可以玩弄它了..!

    【讨论】:

      【解决方案3】:

      读取文件与冒泡排序无关。您可以读取该文件以创建一个整数数组,然后使用通常的冒泡排序算法对其进行排序

      【讨论】:

        【解决方案4】:

        你可以这样做:

        package test;
        
        import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.Arrays;
        import java.util.Scanner;
        
        public class Test {
        
            public static void bubbleSort(int[] num ) {
                int j;
                boolean flag = true;   // set flag to true to begin first pass
                int temp;   //holding variable
        
                while ( flag ) {
                    flag= false;    //set flag to false awaiting a possible swap
                    for( j=0;  j < num.length -1;  j++ ) {
                        if ( num[ j ] < num[j+1] )  {
                            temp = num[ j ];                //swap elements
                            num[ j ] = num[ j+1 ];
                            num[ j+1 ] = temp;
                            flag = true;              //shows a swap occurred  
                        } 
                    } 
                } 
            } 
        
            public static void main(String[] args) throws FileNotFoundException {
                Scanner scanner = new Scanner(new File("numbers.txt"));
                int [] numbers = new int [256];
                int i = 0;
                while(scanner.hasNextInt()){
                   numbers[i++] = scanner.nextInt();
                }
        
                bubbleSort(numbers);
        
                System.out.println(Arrays.toString(numbers));
        
            }
        }
        

        【讨论】:

        • 我尝试了您的解决方案,除输出打印外一切正常 [I@1909752
        • 出了什么问题? :) 我忘记打印了
        猜你喜欢
        • 2015-09-13
        • 2011-04-24
        • 2011-04-22
        • 2012-04-14
        • 2021-06-29
        • 2021-05-11
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        相关资源
        最近更新 更多