【发布时间】: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