[英]Issue trying to create a Bubble Sort using ArrayList<Integer>
Hi I'm trying to figure out how to use BubbleSort in Java and my code is erroring and I don't know why嗨,我想弄清楚如何在 Java 中使用 BubbleSort,但我的代码出错了,我不知道为什么
import java.util.ArrayList;
public class SortsRunner {
public static void BubbleSort(ArrayList<Integer> nums) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = arr.size();
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr.get(j) > arr.get(j+1))
{
int temp = arr.get(j);
arr.get(j) = arr.get(j+1);
arr.get(j+1) = temp;
}
}
public static void SelectionSort(ArrayList<Integer> nums) {
}
public static void printArrayList(ArrayList<Integer> nums) {
for(int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i) + " ");
}
System.out.println();
}
public static ArrayList<Integer> makeRandomArrayList() {
ArrayList<Integer> nums = new ArrayList<>();
for(int i = 0; i < (int)(Math.random() * 11) + 5; i++) {
nums.add((int)(Math.random() * 100));
}
return nums;
}
public static void main(String[] args) {
printArrayList(makeRandomArrayList());
}
} }
When I get to arr.get(j) = arr.get(j+1);
当我到达arr.get(j) = arr.get(j+1);
and arr.get(j+1) = temp;
和arr.get(j+1) = temp;
the left side errors saying "The left-hand side of an assignment must be a variable."左侧错误说“赋值的左侧必须是变量”。 can anyone help me fix this?谁能帮我解决这个问题?
arr.get(j) = arr.get(j+1);
arr.get(j+1) = temp;
You're trying to assign a value to the result of a method call.您正在尝试为方法调用的结果赋值。
You just can't do this.你不能这样做。 You can only assign to a local variable, a field in the current class, a field access (eg foo.bar = ...
) or an array element (eg foo[0] = ...
).您只能分配给局部变量、当前类中的字段、字段访问(例如foo.bar = ...
)或数组元素(例如foo[0] = ...
)。
Instead, you should use set
to update a list element:相反,您应该使用set
来更新列表元素:
arr.set(j, arr.get(j+1));
arr.set(j+1, temp);
For the specific case of swapping two elements around in a list, you can instead use Collections.swap
:对于在列表中交换两个元素的特定情况,您可以改用Collections.swap
:
Collections.swap(arr, j, j+1);
You are doing several things wrong.你做错了几件事。
get
and set
issues already mentioned.已经提到的明显的get
和set
问题。empty list
.您正在对empty list
进行排序的事实。 You pass in nums
but sort the one you create which is empty.您传入nums
但对您创建的空的进行排序。boolean
to prevent unnecessary repeats of the outer loop
.您应该使用boolean
来防止不必要的outer loop
重复。 Think of it like this, if you don't make a swap on the first iteration of the outer loop, then you won't swap on subsequent iterations.可以这样想,如果您不在外循环的第一次迭代中进行交换,那么您将不会在后续迭代中进行交换。 And one style suggestion.和一种风格建议。 Don't use loops
or if statements
without {}
.不要使用loops
或没有{}
if statements
。 Even if they only contain a single line of code.即使它们只包含一行代码。 You will be less likely to make coding errors if you do so.如果这样做,您将不太可能犯编码错误。
Try the following:请尝试以下操作:
public static void BubbleSort(List<Integer> nums) {
int n = nums.size();
for (int i = 0; i < n; i++) {
boolean swapped = false;
for (int j = 0; j < n-1; j++) {
if (nums.get(j) > nums.get(j + 1)) {
int temp = nums.get(j);
nums.set(j, nums.get(j + 1));
nums.set(j + 1, temp);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.