繁体   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