gpt4 book ai didi

java - 使用冒泡排序对数组进行排序,发现错误 - 需要 : variable,:值

转载 作者:行者123 更新时间:2023-12-02 06:21:23 24 4
gpt4 key购买 nike

我尝试使用冒泡排序对数组进行排序,但它不起作用。它说“必需:变量,找到:值”

Array.java:143: error: unexpected type
temp.get(d) = temp.get(d+1);

^

required: variable
found: value


Array.java:144: error: unexpected type
temp.get(d+1) = swap;

^

required: variable
found: value



这样Main就给定了,我需要做的就是写这个冒泡排序函数


这是我写的

public static void bubbleSort(Array<Integer> lista){
boolean swapped;
Array<Integer> temp;
int n;

n= temp.size;

int swap;

for (int c = 0; c < ( n - 1 ); c++) {
for (int d = 0; d < n - c - 1; d++) {
if (temp.get(d) > temp.get(d+1))
{
swap = temp.get(d);
temp.get(d) = temp.get(d+1);
temp.get(d+1) = swap;
}
}
}
}



这是给出的 Main

public static void main(String[] args) throws IOException{
BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
Array<Integer> niza =new Array<Integer>(N);
bubbleSort(niza);

for(int i=0;i<N;i++){
s = stdin.readLine();
niza.set(i, Integer.parseInt(s));
}

System.out.println(brojDoProsek(niza));
}


如何修复此错误?

最佳答案

假设 Array 实际上是 ArrayList,并且假设您要将索引 d + 1 处的值存储在索引 d 中,您需要

temp.set(d, temp.get(d + 1));

temp.get(d) 不是可以为其赋值的变量。这是一个返回值的表达式。并且您无法将任何内容分配给值。

关于java - 使用冒泡排序对数组进行排序,发现错误 - 需要 : variable,:值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20978133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com