冒泡排序,使用将引用作为参数的交换函数,出现运行时错误。

huangapple go评论54阅读模式
英文:

Bubble sort with swap function having reference as parameter, getting runtime error

问题

我为冒泡排序编写了一个简单的代码,并使用了一个接受对象引用作为参数的交换函数。我遇到了以下错误:

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at Main.main(Main.java:6)

我的代码:

import java.util.Scanner;   
public class Bubble {   
	public static void main(String[] args){   
		Scanner scan = new Scanner(System.in);    
		int n = scan.nextInt();   
		int[] arr = new int[n];   
		for(int i = 0; i < n ; i++){  
			arr[i] = scan.nextInt();   
		}  
		bubbleSort(arr);  
		for(int i = 0; i < n ; i++){  
			System.out.print(arr[i] + " ");  
		}   
	}  
	static int[] bubbleSort(int[] arr){
		int swapped = 1;
		while(swapped != 0){
			swapped = 0;
			for(int i = 1; i < arr.length; i++){
				if(arr[i - 1] > arr[i]){
					Example a = new Example(arr[i - 1]);
					Example b = new Example(arr[i]);
					swap(a, b);
					swapped++;
				}
			}
		}
		return arr;
	}

	static void swap(Example a, Example b){
		int temp = a.a;
		a.a = b.a;
		b.a = temp;
	}
}
class Example{
	int a ;
	public Example(int a){
		this.a = a;
	}
}

我该如何捕获这些错误,为什么会出现这些错误?

英文:

I wrote a simple code for bubble sort and used a swap function which accepts reference of objects as its parameter.
I am getting the following error:

Exception in thread &quot;main&quot; java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:6)

My code:

import java.util.Scanner;   
public class Bubble {   
public static void main(String[] args){   
Scanner scan = new Scanner(System.in);    
int n= scan.nextInt();   
int[] arr = new int[n];   
for(int i = 0; i&lt;n ; i++){  
arr[i] = scan.nextInt();   
}  
bubbleSort(arr);  
for(int i = 0; i&lt;n ; i++){  
System.out.print(arr[i]+&quot;&quot;);  
}   
}  
static int[] bubbleSort(int[] arr){
int swapped = 1;
while(swapped!= 0){
swapped = 0;
for(int i=1; i&lt; arr.length; i++){
if(arr[i-1]&gt;arr[i]){
Example a = new Example(arr[i-1]);
Example b = new Example(arr[i]);
swap(a,b);
swapped++;
}
}
}
return arr;
}
static void swap(Example a, Example b){
Example temp = new Example(0) ;
temp.a = a.a;
a.a = b.a;
b.a= temp.a;
}
}
class Example{
int a ;
public Example(int a){
this.a = a;
}
}

How can I catch these errors and why am I getting these?

答案1

得分: 0

你对 Example 的操作是不起作用的。当你执行:

Example a = new Example(arr[i-1]);

你的 Example a 只包含了 arr[i-1] 的值。对 a.a 的修改不会影响你的数组。所以你的数组永远不会变成有序的,而且会陷入无限循环。

如果你想要一个交换数组中元素的方法,最简单的方式是将数组传递给方法。

static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

然后你可以使用以下方法来交换元素:

swap(arr, i-1, i);
英文:

What you're doing with Example will not work. When you do

Example a = new Example(arr[i-1]);

Your Example a only contains the value of arr[i-1]. Altering a.a has no effect on your array. So your array never becomes sorted and you have an infinite loop.

If you want a method to swap elements in your array, the easiest way is to pass the array to the method.

static void swap(int[] arr, i, j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

Then you can swap elements with

swap(arr, i-1, i);

huangapple
  • 本文由 发表于 2020年8月30日 14:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定