我正在做一件事情,我必须“将list2中的每个元素与其元素索引号相乘,然后将值放在数组list1中。” 所以我的代码是
public static void main(String[] args) {
int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int L1= 0;
int L2= 0;
for( L2=0 ;L2<List2.length;L2++) {
List2[L2]= (int) (100*Math.random());
System.out.print(" "+List2[L2]);
}//end of list2 for loop
System.out.println("");
for( L1 = 0;L1<List1.length;L1++)
{
List1[L1]= List2[L2]*L2;
System.out.print(" "+List1[L1]);
}//end of list1 for loop
)
并引发此错误“线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:索引20超出长度20的范围”
答案 0 :(得分:0)
您的List1实际上包含21个元素(请注意最后一个“,”,这会添加另一个元素),而List2仅包含20个元素。因此,在第二个循环中,您将迭代21次,当您到达21st时, List2不再包含任何元素。
答案 1 :(得分:0)
您的第一个列表包含21个元素,第二个列表包含20个元素。在第二个循环中,您访问第二个列表的21个元素。这样您就得到了例外。
在声明List1的末尾注意逗号:
int List1 [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
int List2 [] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
您可能想执行以下操作:
int List1 [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int List2 [] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
但是看一下代码-如果您只想用空值初始化一个数组,就可以
int[] list = new int[20];
您的代码中还有其他问题。在您的第一个循环之后,L2将始终为List2(20)的大小。因此,在List1[L1] = List2[L2] * L2;
的第二个循环中,您正在访问List2的索引20处的元素-该元素不存在,因为在size = 20处,最大索引为19。
所以-我不知道您想用代码实现什么,但是这种方式行不通。
答案 2 :(得分:0)
第二个循环中有一个异常。您尝试致电List2[20]
。 List2只有0到19的索引可用,并且您正在尝试访问第20个不可用的元素。
List1[L1]= List2[L2]*L2;
尝试这样。
List1[L1]= List2[L1]*L2;
完整代码。
public static void main(String[] args) {
int List1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
int List2[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
int L1 = 0;
int L2 = 0;
for (L2 = 0; L2 < List2.length; L2++) {
List2[L2] = (int) (100 * Math.random());
System.out.print(" " + List2[L2]);
} // end of list2 for loop
System.out.println("");
System.out.println(List1.length + " " + List2.length);
for (L1 = 0; L1 < List1.length; L1++) {
List1[L1] = List2[L1] * L2;
System.out.print(" " + List1[L1]);
} // end of list1 for loop
}
答案 3 :(得分:0)
因为在行 List1 [L1] = List2 [L2] xL2; * ,L2 = 20,但数组的最大索引为19。您必须编写:
public static void main(String[] args) {
int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int L1= 0;
int L2= 0;
for( L2=0 ;L2<List2.length;L2++) {
List2[L2]= (int) (100*Math.random());
System.out.print(" "+List2[L2]);
}//end of list2 for loop
System.out.println("");
for( L1 = 0;L1<List1.length;L1++)
{
List1[L1]= List2[L2 - 1]*L2; //Here the change
System.out.print(" "+List1[L1]);
}//end of list1 for loop
}
或
public static void main(String[] args) {
int List1 []= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
int List2 []= {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int L1= 0;
int L2= 0;
for( L2=0 ;L2<List2.length;L2++) {
List2[L2]= (int) (100*Math.random());
System.out.print(" "+List2[L2]);
}//end of list2 for loop
System.out.println("");
for( L1 = 0;L1<List1.length;L1++)
{
List1[L1]= List2[L1]*L2; //Here the change
System.out.print(" "+List1[L1]);
}//end of list1 for loop
}