[英]Array Index Out Of Bounds Exception
問題:
放學期結束后更新學生成績列表的程序。 該程序應從輟學的學生人數及其索引中讀取數據,然后該程序應將其余學生的成績復制到新數組中。 此外,該程序應同時顯示原始列表和更新列表。 [提示:新數組的長度必須等於剩余學生的數量]
我的代碼:
public class Q5{
static Scanner scan = new Scanner (System.in);
public static void main (String args[]){
double [] list={1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10};
System.out.println("enter number of students who dropped:");
int num=scan.nextInt();
double [] list2 = new double [num];
System.out.println("Enter index Of the student who dropped ");
for (int j=1 ; j<=num ; j++)
{
System.out.println("student" + j + ":");
int index=scan.nextInt();
list[index]=0;
}
int j=0;
for(int i=0; i<list.length ; i++)
if (list[i]!=0)
{
list2[j]=list[i];
j++;
}
System.out.print("The original list : " );
for(int i=0; i<list.length ; i++)
System.out.print(list[i] + " " );
System.out.print("remaining students " );
for(int i=0; i<list2.length ; i++)
System.out.print(list2[i] + " " );
}
}
問題是什么 ? 它不起作用! 在第16行中說:
線程“主”中的異常java.lang.ArrayIndexOutOfBoundsException:Q5.main處為4(Q5.java:23)
我該如何糾正
那是因為list2
的大小。
您不應將其大小設置為num
,而應將其設置為原始列表的大小。 因為list2
將包含與原始list
一樣多的元素。 您只需要num
即可從用戶那里獲取許多輸入並為這些索引分配值0 。
double[] list2 = new double[list.length]; // This should be the size of your list2
如果不需要保留原始大小,則需要按照@Joetjah的建議從原始大小中減去num。
double[] list2 = new double[list.length - num]; // This should be the size of your list2
如果用戶輸入的值超出數組的范圍,則您的代碼將失敗:
int index=scan.nextInt();
list[index]=0; // This may fail. Value entered by the user may exceed the array length
偽代碼:
您從10名學生開始。 假設您要刪除2個學生。 這意味着您將list2
大小設置為2。
接下來,您嘗試將所有學生(10)從list
到list2
。 但是, list2
太小了。 這樣就得到了例外。
您想要的是:
double [] list2 = new double [list.length - num];
使用預定義的數組“列表”來猜測該問題。
步驟1:使用初始化方法之一創建列表。 在這里大小是固定的。
步驟2:接受用戶輸入:假設它是11。
列表不需要包含輸入的大小。
您使用錯誤的大小初始化了list2
。
您從被放棄的學生的用戶數中獲取,然后嘗試將所有剩余的學生放入list2
。
double [] list2 = new double [list.length - num];
工作正常
導入java.util.Scanner;
public class Q5 {
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println("enter number of students who dropped:");
int num = scan.nextInt();
double[] list2 = new double[num - 1]; // should be num-1
System.out.println("Enter index Of the student who dropped ");
for (int j = 0; j < num; j++) {
System.out.println("student" + j + ":");
int index = scan.nextInt();
list[index] = 0;
}
int j = 0;
for (int i = 0; i < num; i++) {
if (list[i] != 0) {
list2[j] = list[i];
j++;
}
}
System.out.print("The original list : ");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.print("remaining students ");
for (int i = 0; i < list2.length; i++) {
System.out.print(list2[i] + " ");
}
}
}
輸出是
enter number of students who dropped:
2
Enter index Of the student who dropped
student0:
1
student1:
2
The original list : 1.0 0.0 0.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 remaining students 1.0
這將刪除您遇到的異常。 從下次開始,您就不再使用靜態值。
編輯:最好不要自己將值分配給任何變量。 由於數組的大小為10。如果用戶輸入的學生人數超過10,則將導致問題。
import java.util.*;
public class Q5 {
static Scanner scan = new Scanner(System.in);
public static void main(String args[]) {
double[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println("enter number of students who dropped:");
int num = scan.nextInt();
double[] list2 = new double[list.length-num]; // should be num-1
System.out.println("Enter index Of the student who dropped ");
for (int j = 0; j < num; j++) {
System.out.println("student" + j + ":");
int index = scan.nextInt();
list[index] = 0;
}
int j = 0;
for (int i = 0; i < list.length; i++) {
System.err.println(""+list[i]);
if (list[i] > 0) {
list2[j] = list[i];
j++;
}
}
System.out.print("The original list : ");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
System.out.print("remaining students ");
for (int i = 0; i < list2.length; i++) {
System.out.print(list2[i] + " ");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.