Java 在实现bubbleSort后未获得正确的输出
该程序创建一个名为datafile.txt的文件,并使用文本I/O将随机创建的100个整数写入该文件。我还实现了bubbleSort以升序对数字进行排序,但它没有对数字进行排序。另外,命令行的输出是“排序的数字是:[I@f72617“100次,谢谢你Java 在实现bubbleSort后未获得正确的输出,java,io,bubble-sort,Java,Io,Bubble Sort,该程序创建一个名为datafile.txt的文件,并使用文本I/O将随机创建的100个整数写入该文件。我还实现了bubbleSort以升序对数字进行排序,但它没有对数字进行排序。另外,命令行的输出是“排序的数字是:[I@f72617“100次,谢谢你 import java.io.*; import java.util.Random; public class Lab5 { //sort array static int[] bubbleSort(int[] array)
import java.io.*;
import java.util.Random;
public class Lab5 {
//sort array
static int[] bubbleSort(int[] array) {
for (int pass = 1; pass <= 100; pass++) {
for (int current = 0; current < 100-pass; current++) {
//compare element with next element
if (array[current] > array[current + 1]) {
//swap array[current] > & array[current + 1]
int temp = array[current];
array[current] = array[current + 1];
array[current + 1] = temp;
} //end if
}
}
return array;
}
public static void main(String args[]) {
//Open file to write to
try {
FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");
int index = 0;
//Convert FileOutputStream into PrintStream
PrintStream myOutput = new PrintStream(fout);
Random numbers = new Random();
//Declare array
int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
//get the int from Random Class
array[i] = (numbers.nextInt(100) + 1);
myOutput.print(array[i] + " ");
//sort numbers
int[] sortedArray = bubbleSort(array);
//print sorted numbers
System.out.print("The sorted numbers are: ");
System.out.print(sortedArray);
}
}
catch (IOException e) {
System.out.println("Error opening file: " + e);
System.exit(1);
}
}
import java.io.*;
导入java.util.Random;
公共类Lab5{
//排序数组
静态int[]bubbleSort(int[]数组){
for(int pass=1;pass数组[current+1]){
//交换阵列[current]>&阵列[current+1]
int temp=阵列[当前];
数组[当前]=数组[当前+1];
阵列[当前+1]=温度;
}//如果结束,则结束
}
}
返回数组;
}
公共静态void main(字符串参数[]){
//打开要写入的文件
试一试{
FileOutputStream fout=新的FileOutputStream(“F:\\IT311\\datafile.txt”);
int指数=0;
//将FileOutputStream转换为PrintStream
PrintStream myOutput=新的打印流(fout);
随机数=新随机数();
//声明数组
int数组[]=新的int[100];
for(int i=0;i
}使用以下内容:
System.out.print(Arrays.toString(sortedArray));
而不是
System.out.print(sortedArray);
使用
第一个是打印数组的地址。第二个将打印数组的每个元素。我对您的代码做了一些更改,您在每次迭代时都打印数组对象。要打印数组,您需要迭代数组并打印其每个元素。 另外,冒泡排序被修改了&如果您想使用的话,还有附加代码,只是不注释而已
import java.io.*;
import java.util.Arrays;
import java.util.Random;
public class Lab5 {
//sort array
static int[] bubbleSort(int[] array) {
for (int pass = 0; pass < 100; pass++) {
for (int current = 1; current < 100-pass; current++) {
//compare element with next element
if (array[current-1] > array[current ]) {
//swap array[current] > & array[current + 1]
int temp = array[current-1];
array[current-1] = array[current];
array[current ] = temp;
} //end if
}
}
return array;
}
public static void main(String args[]) {
//Open file to write to
try {
FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");
int index = 0;
//Convert FileOutputStream into PrintStream
PrintStream myOutput = new PrintStream(fout);
Random numbers = new Random();
// code begin to print random number in file and output sorted array on sysout
//Declare array
int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
//get the int from Random Class
array[i] = (numbers.nextInt(100) + 1);
myOutput.print(array[i] + " ");
}
//sorted array using sort function
// Arrays.sort(array);
// sortred using bubble sort
array=bubbleSort(array);
//print sorted numbers
System.out.print("The sorted numbers are: ");
for (int i = 0; i < array.length; i++)
{
int j = array[i];
System.out.print(j +" " );
}
// code end to print random number in file and output sorted array on sysout
// code begin to print sorted number in file and output sorted array on sysout
/*int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = i;
}
Arrays.sort(array);
for (int j = 0; j < array.length; j++)
{
int j2 = array[j];
myOutput.print(j2 + " ");
System.out.println(j2+" ");
}
*/
// code end to print sorted number in file and output sorted array on sysout
}
catch (IOException e) {
System.out.println("Error opening file: " + e);
System.exit(1);
}
}
}
import java.io.*;
导入java.util.array;
导入java.util.Random;
公共类Lab5{
//排序数组
静态int[]bubbleSort(int[]数组){
对于(int pass=0;pass<100;pass++){
用于(int电流=1;电流<100次;电流++){
//将元素与下一个元素进行比较
if(数组[current-1]>数组[current]){
//交换阵列[current]>&阵列[current+1]
int temp=阵列[current-1];
数组[current-1]=数组[current];
阵列[当前]=温度;
}//如果结束,则结束
}
}
返回数组;
}
公共静态void main(字符串参数[]){
//打开要写入的文件
试一试{
FileOutputStream fout=新的FileOutputStream(“F:\\IT311\\datafile.txt”);
int指数=0;
//将FileOutputStream转换为PrintStream
PrintStream myOutput=新的打印流(fout);
随机数=新随机数();
//代码开始打印文件中的随机数,并在sysout上输出排序数组
//声明数组
int数组[]=新的int[100];
for(int i=0;i
谢谢。我收到一个错误,“数组”上的“找不到符号系统.out.print(Arrays.toString(sortedArray));”那是因为它的数组,你需要导入java.util.ArrayShanks。那是编译过的,但数字没有排序。哦,对不起。它们在cmd行排序,但不在输出文件中。哦,没关系。我不需要在输出文件中排序。谢谢你的帮助!!!谢谢你所有的时间和精力。我接受了zerocool的建议,“您应该将int[]sortedArray=bubbleSort(数组);保持在for循环之外。反复调用没有意义。只需调用一次它就可以对所有100个元素进行排序。”它成功了。
import java.io.*;
import java.util.Arrays;
import java.util.Random;
public class Lab5 {
//sort array
static int[] bubbleSort(int[] array) {
for (int pass = 0; pass < 100; pass++) {
for (int current = 1; current < 100-pass; current++) {
//compare element with next element
if (array[current-1] > array[current ]) {
//swap array[current] > & array[current + 1]
int temp = array[current-1];
array[current-1] = array[current];
array[current ] = temp;
} //end if
}
}
return array;
}
public static void main(String args[]) {
//Open file to write to
try {
FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");
int index = 0;
//Convert FileOutputStream into PrintStream
PrintStream myOutput = new PrintStream(fout);
Random numbers = new Random();
// code begin to print random number in file and output sorted array on sysout
//Declare array
int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
//get the int from Random Class
array[i] = (numbers.nextInt(100) + 1);
myOutput.print(array[i] + " ");
}
//sorted array using sort function
// Arrays.sort(array);
// sortred using bubble sort
array=bubbleSort(array);
//print sorted numbers
System.out.print("The sorted numbers are: ");
for (int i = 0; i < array.length; i++)
{
int j = array[i];
System.out.print(j +" " );
}
// code end to print random number in file and output sorted array on sysout
// code begin to print sorted number in file and output sorted array on sysout
/*int array[] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = i;
}
Arrays.sort(array);
for (int j = 0; j < array.length; j++)
{
int j2 = array[j];
myOutput.print(j2 + " ");
System.out.println(j2+" ");
}
*/
// code end to print sorted number in file and output sorted array on sysout
}
catch (IOException e) {
System.out.println("Error opening file: " + e);
System.exit(1);
}
}
}