gpt4 book ai didi

Java错误: illegal start of the expressions

转载 作者:行者123 更新时间:2023-12-01 19:31:38 26 4
gpt4 key购买 nike

public class Flatten {
public static int[] flatten(int[][] x) {
int totalLength = 0;

for (int i = 0; i < x.length; i++) {
if (x[i].length > 0) {
for (int j = 0; j < x[i].length; j++) {
totalLength += 1;
}
}
}

int[] a = new int[totalLength];
int aIndex = 0;

for (int i = 0; i < x.length; i++) {
if (x[i].length > 0) {
for (int j = 0; j < x[i].length; j++) {
a[aIndex] = x[i][j];
aIndex += 1;
}
}
}

return a;
}

public static void main(String[] args) {
int[] test = flatten({{1, 2, 3}, {}, {7, 8}}); // Error line
for(int i = 0; i < test.length; i++){
System.out.print(test[i] + " ");
}
}
}

大家好!这是我的作业文件中的一个简短代码。当我运行代码时,终端显示“=”是非法的。我不知道问题出在哪里。希望你们能帮我找出答案:)

最佳答案

唯一可以在数组初始值设定项上省略类型的情况是在声明数组变量时。例如,

int[] test = flatten({{1, 2, 3}, {}, {7, 8}});

可能

int[][] tempArr = {{1, 2, 3}, {}, {7, 8}};
int[] test = flatten(tempArr);

int[] test = flatten(new int[][] {{1, 2, 3}, {}, {7, 8}});

关于Java错误: illegal start of the expressions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59656880/

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