假设这是我的代码:
public class Test {
public static void main(String[] args) {
int[] text = new int[0];
for (int i = 0; v<example.length(); i++) {
int text[] = {maybe.indexOf(example)
};
}
}
如何才能使 for
循环内的数组覆盖之前初始化的数组 text[]
?现在,我收到错误消息:Java 在我的 for
循环中找不到符号 text3
。我想在数组 text[]
具有在 for
循环中确定的所有值之前对其进行初始化,以便我可以将其用于程序的其余部分。
最佳答案
只需写信给它:
public class Test {
public static void main(String[] args) {
int[] text = new int[example.length()];
for (int i = 0; i < example.length(); i++) {
text[i] = maybe.indexOf(example);
}
}
}
text[i]
写入数组中的 ith+1
位置(C 样式数组从 0 开始计数 - 由于涉及内存引用位置的复杂原因)。
关于java - 在先前初始化数组后如何使用数组(并为其赋值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52583140/