java.lang.ArrayIndexOutOfBoundsException: 索引 20 超出长度 20 的界限

huangapple 未分类评论62阅读模式
标题翻译

java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20

问题

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] * L1;
        System.out.print(" " + List1[L1]);
    } //end of list1 for loop
}

It throws this error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20"

英文翻译

Im doing a thing for a thing and I have to "Multiply each element in list2 by its element index number and place the value in array list1."
and so my code is

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&lt;List2.length;L2++) {
			List2[L2]= (int) (100*Math.random());
			System.out.print(&quot; &quot;+List2[L2]);
		}//end of list2 for loop

System.out.println(&quot;&quot;);
		
		for( L1 = 0;L1&lt;List1.length;L1++)
		{
			List1[L1]= List2[L2]*L2;
			System.out.print(&quot; &quot;+List1[L1]);
		}//end of list1 for loop
)

and it throws this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20"

答案1

得分: 0

你的List1实际上包含21个元素(注意最后的“,”,它添加了另一个元素),而List2只包含20个。因此,在第二个循环中,你循环了21次,当你到达第21个时,List2不再包含元素。

英文翻译

Your List1 actually contains 21 elements (mind the last ",", which adds another element) while List2 contains only 20. So, in the second loop, you're iterating 21 times, and when you get to the 21st, List2 contains no more elements.

答案2

得分: 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处的元素——但这个元素不存在,因为在大小为20时,你的最大索引是19。

所以——我不知道你的代码意图是什么,但它以这种方式是行不通的。

英文翻译

Your first list has 21 elements, your second list has 20 elements. In your second loop you access the 21 element of the second list. So you get the exception.

notice the comma at the end of your declaration of 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};

You probably wanted to do the following:

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};

But looking at your code - if you just want to initialize an array with empty values you just can do

int[] list = new int[20];

And you got other problems in your code. After your first loop L2 will always be the size of List2 (20). So in your second loop in List1[L1] = List2[L2] * L2; you are accessing the element at index 20 of List2 - that doesn't exist, because at size=20 your maximum index is 19.

So - I don't know what you try to achieve with your code, but it won't work this way.

答案3

得分: 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
}
英文翻译

You have an exception in your second loop. you try to call List2[20]. List2 only have 0 to 19 index available and you are trying to access 20th element which is not available.

List1[L1]= List2[L2]*L2;

Try like this.

List1[L1]= List2[L1]*L2;

complete code.

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 &lt; List2.length; L2++) {
		List2[L2] = (int) (100 * Math.random());
		System.out.print(&quot; &quot; + List2[L2]);
	} // end of list2 for loop

	System.out.println(&quot;&quot;);
	System.out.println(List1.length + &quot; &quot; + List2.length);

	for (L1 = 0; L1 &lt; List1.length; L1++) {
		List1[L1] = List2[L1] * L2;
		System.out.print(&quot; &quot; + List1[L1]);
	} // end of list1 for loop
}

答案4

得分: 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++)
    {
        if (L2 > 0) {
            List1[L1]= List2[L2 - 1]*L2; //Here the change
        } else {
            List1[L1]= 0; // Handle the case when L2 is 0
        }
        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
}
英文翻译

Because at line List1[L1]= List2[L2]xL2;* L2 = 20, but the max index of the array is 19. You must write:

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&lt;List2.length;L2++) {
        List2[L2]= (int) (100*Math.random());
        System.out.print(&quot; &quot;+List2[L2]);
    }//end of list2 for loop

    System.out.println(&quot;&quot;);

    for( L1 = 0;L1&lt;List1.length;L1++)
    {
        List1[L1]= List2[L2 - 1]*L2; //Here the change
        System.out.print(&quot; &quot;+List1[L1]);
    }//end of list1 for loop
}

or

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&lt;List2.length;L2++) {
        List2[L2]= (int) (100*Math.random());
        System.out.print(&quot; &quot;+List2[L2]);
    }//end of list2 for loop

    System.out.println(&quot;&quot;);

    for( L1 = 0;L1&lt;List1.length;L1++)
    {
        List1[L1]= List2[L1]*L2; //Here the change
        System.out.print(&quot; &quot;+List1[L1]);
    }//end of list1 for loop
}

huangapple
  • 本文由 发表于 2020年5月30日 23:20:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/62104552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定