更新时间:2023-09-11 21:28:04
首先,SIZE被声明为一个整数final int SIZE = 5 ;但是你试图将它用作两者for循环行中的整数:
for ( int i = 0 ; i< = SIZE; i ++);以分号结尾,所以
i
超出范围,以后不能使用 - 并且会尝试访问不存在的元素:当你声明一个n $的数组时c $ c>元素,索引开始为零,因此您可以访问元素[0],[1],...,[n - 1]但不能访问[n],因为它不存在。
并且还使用SIZE作为后面一行中的数组:SIZE [i] = i;它不能同时出现!
下一行没问题!int A [] [] = {{ 1 , 2 },{ 3 , 4 }};
然后再次出错,使用错误的语法:A [ 1 , 1 ] = 5 ;
试试这个:final int SIZE = 5 跨度>;
int data [] = new int [SIZE];
for ( int i = 0 ; i< SIZE; i ++){
data [i] = i;
}
int A [] [] = {{ 1 , 2 },{ 3 , 4 }};
A [ 1 ] [ 1 ] = 5 ;
for ( int i = 0 ; i< = SIZE; i ++); // 由于此处有分号,
SIZE [i] = i; // 此行不在循环中
final int SIZE = 5;
for (int i = 0; i <= SIZE; i++);
SIZE [i] = i;
int A [] [] = {{1,2},{3,4}};
A [1,1] = 5;
What I have tried:
I have tried using a different variable
Firstly, SIZE is declared as an integerfinal int SIZE = 5;But you are trying to use it as both an integer in your for loop line:
for (int i = 0; i <= SIZE; i++);Which ends at the semicolon, so
i
goes out of scope and can't be used later - and would try to access elements that don't exist: when you declare an array ofn
elements, the indexes start are zero, so you can access elements [0], [1], ..., [n - 1] but not [n] as it doesnt exist.
And also use SIZE as an array in the line after it:SIZE [i] = i;It can't be both at the same time!
The next line is fine!int A [] [] = {{1,2},{3,4}};
But then you go wrong again, by using it with the wrong syntax:A [1,1] = 5;
Try this:final int SIZE = 5; int data[] = new int[SIZE]; for (int i = 0; i < SIZE; i++){ data[i] = i; } int A[][] = { {1, 2}, {3, 4} }; A[1][1] = 5;
for (int i = 0; i <= SIZE; i++); // Because of semicolon here, SIZE [i] = i; // this line is not in the loop