簡體   English   中英

不知道為什么會出現數組越界異常

[英]Don't know why the array out of bounds exception occurs

這是我試圖解決的問題:將字符串“WELCOMETOZOHOCORPORATION”保存在二維數組中,並從左到右和從上到下在二維字符串中搜索像“too”這樣的子字符串。

WELCO/n METOZ/n OHOCO/n RPORA/n TION
這里的 /n 用於表示下一行並將開始和結束索引打印為

開始索引:<1,2>

結束索引:<3, 2> 但我不知道為什么會發生錯誤。!

public class Try1 {
public static void main(String[] args) {
       Scanner sc= new Scanner (System.in);
        System.out.println("Enter the string");
        String str=sc.next();
        char arr[][]=new char[5][5];
        char a[]=str.toCharArray();
        int l=a.length;
        int k=0,flag=0;
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                if(k!=l){
                arr[i][j]=a[k];
                k++;}else{
                    break;
                }
            }
        }
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                System.out.print(arr[i][j]+"  ");
            }
            System.out.println();
        }
        System.out.println("Enter the string to search");
        String str1=sc.next();
        char b[]=str1.toCharArray();
        int l1=b.length,y=0,count=0,rl=0,td=0,v=l1-1;
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                if(arr[i][j]==b[y])//THIS IS THE LINE WHERE THE ERROR OCCURS
                {
                    count++;
                    for(y=1;y<l1;y++){
                        if(arr[i][j+y]==b[y]){
                            rl=count+rl;
                            if(rl==l1){
                                flag=1;
                                System.out.println("Start Index: "+i+","+j);
                                System.out.println("End Index: "+i+","+(j+v));
                                break;
                            }
                        }else if(arr[i+y][j]==b[y]){
                            td=count+td;
                            if(td==l1){
                                flag=1;
                                System.out.println("Start Index: "+i+","+j);
                                System.out.println("End Index: "+(i+v)+","+j);
                                break;
                            }
                        }
                    }
                }
            }
        }
        if(flag==0){
            System.out.println("Not Found");
        }
    }
The error i am facing is,
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at try1.Try1.main(Try1.java:48)
Could you help me guys.
                if(arr[i][j]==b[y])//THIS IS THE LINE WHERE THE ERROR OCCURS

問題在於b[y] 第一次通過循環y為0,沒問題。 幾行之后在一個內部循環中你做

                    for(y=1;y<l1;y++){

這個循環使y等於l1 ,即搜索的單詞的長度(在TOO的情況下為 3 )。 因此,第二次到達發生錯誤的行時,示例中的y為 3,並且您嘗試與長度為 3 的數組中索引 3 處的元素進行比較。這會導致異常(如您所知,數組索引是從 0 開始的,因此數組中的有效索引是 0、1 和 2)。

我不明白你的代碼應該如何工作,所以猶豫是否提出修復建議。 如果合適,您可以在外循環的每次迭代中將y設置回 0。

你得到的錯誤,因為數組中的最大指數, b為字符串TOO2 ,當您試圖訪問一個元素超越指數2

請按以下步驟操作:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String str = sc.next();
        char arr[][] = new char[5][5];
        char a[] = str.toCharArray();
        int l = a.length;
        int k = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (k != l) {
                    arr[i][j] = a[k];
                    k++;
                } else {
                    break;
                }
            }
        }
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println();
        }
        System.out.print("Enter the string to search: ");
        String str1 = sc.next();
        char b[] = str1.toCharArray();
        int i, j, countH = 0, countV = 0;
        boolean found = false;
        for (i = 0; i < 5; i++) {
            countH = countV = 0;
            for (j = 0; j < 5 && countH < b.length && countV < b.length; j++) {
                if (arr[i][j] == b[countH]) {
                    countH++;
                } else if (arr[j][i] == b[countV]) {
                    countV++;
                }
            }
            if (countH == b.length) {
                found = true;
                System.out.println("Found horizontally starting at index " + "[" + (i) + "][" + (j - b.length) + "]");
            }
            if (countV == b.length) {
                found = true;
                System.out.println("Found vertically starting at index " + "[" + (j - b.length) + "][" + i + "]");
            }
        }
        if (!found) {
            System.out.println("Not Found");
        }
    }
}

示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N    
Enter the string to search: TOO
Found vertically starting at index [1][2]

另一個示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N   
Enter the string to search: ABC
Not Found

另一個示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N    
Enter the string to search: ZOA
Found vertically starting at index [1][4]

另一個示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N    
Enter the string to search: MET
Found horizontally starting at index [1][0]

另一個示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N    
Enter the string to search: PORA
Found horizontally starting at index [3][1]

另一個示例運行:

Enter the string: WELCOMETOZOHOCORPORATION
W  E  L  C  O  
M  E  T  O  Z  
O  H  O  C  O  
R  P  O  R  A  
T  I  O  N    
Enter the string to search: PORB
Not Found

暫無
暫無

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

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