簡體   English   中英

無法將元素掃描到數組中

[英]Unable to scan the elements into array

我是Java的初學者。 我寫了幾行代碼,它顯示了錯誤:

線程“主”中的異常java.lang.ArrayIndexOutOfBoundsException:0

import java.util.Scanner;
public class Issue {
    public static Scanner scan = new Scanner(System.in);
    int n;
    int ID[]=new int[n];
    String [] Name=new String[n];
    public void get()
    {

        int ID[] = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Enter " + (i+1) + "st Employe ID :");
            ID[i] = scan.nextInt();
            System.out.println("Enter Employe Name:");
            Name[i]=scan.nextLine();

        }
    }
    public static void main(String[] args) {
        Issue obj=new Issue();
        System.out.println("Enter no.of Employes:");
        obj.n=scan.nextInt();
        obj.get();

    }
}

在創建類Issue的對象時,n的值為0,因此創建了大小為0的ID和Name數組。現在,從用戶那里獲取輸入后,只需將n設置為用戶輸入,但ID []和Name []數組的大小仍然為0。因此,在get方法內部,您將訪問0大小數組中的開箱即用索引。

這是更正的代碼:

import java.util.Scanner;
public class Issue {
    public static Scanner scan = new Scanner(System.in);
    int n;
    int ID[];
    String [] Name;
    public void get()
    {
        for (int i = 0; i < n; i++) {
            System.out.println("Enter " + (i+1) + "st Employe ID :");
            ID[i] = scan.nextInt();
            System.out.println("Enter Employe Name:");
            Name[i]=scan.nextLine();
        }
    }
    public static void main(String[] args) {
        Issue obj=new Issue();
        System.out.println("Enter no.of Employes:");
        obj.n=scan.nextInt();
        obj.Name =new String[n];
        obj.ID = new int[n];
        obj.get();

    }
}

在上面的代碼中,我僅對ArrayIndexOutOfBound進行了更正。 但是,可以通過遵循以下良好的編程習慣來改進您的代碼:

  1. 遵循方法和變量的命名約定。
  2. 將類的非最終成員對象設為私有,並使用getter和setter方法。
  3. 遵循關注規則的分離。
  4. 使用BufferedReader而不是Scanner可以提高性能。

這有點棘手,因為盡管您使用obj.n=scan.nextInt(); n的數字,因為數組的大小已用dafult n值0初始化,所以其大小保持0 0。

該行:

obj.n=scan.nextInt();

不保證為數組IDName重新分配內存。 我建議您避免使用公共變量,並使用構造函數進行總數封裝。

public static final Scanner scan = new Scanner(System.in);

private final int n;
private final int ID[];
private final String[] Name;

public Main(int number) {
    this.n = number;
    this.ID = new int[n];
    this.Name = new String[n];
}

public void get() {

    for (int i = 0; i < n; i++) {
        System.out.println("Enter " + (i+1) + "st Employe ID :");
        ID[i] = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter Employe Name:");
        Name[i] = scan.nextLine();

    }
}

public static void main(String[] args) {
    System.out.println("Enter no.of Employes:");
    Main obj = new Main(scan.nextInt());
    obj.get();
}

此外,您必須調用scan.nextLine(); 消耗行本身,因為Scanner::nextInt不會像Scanner::nextLine一樣終止行。

線程“主”中的異常java.lang.ArrayIndexOutOfBoundsException:0

當您嘗試以大於數組大小的索引獲取元素時發生。

例如,

您有一個大小為10的數組A ,並且正在訪問A [10] ,則將獲得異常,因為

  • 數組大小為10,因此有效索引為0-9以訪問數組元素
  • A [10]表示訪問大於數組大小(10)的第(10 + 1)個元素

在您的情況下,當變量n的值為0時,您已經初始化了數組IDName 。要解決此錯誤,應在變量n初始化后初始化這些數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM