有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不知道为什么会发生数组越界异常

这就是我试图解决的问题: 将字符串“WELCOMETOZOHOCORPORATION”保存在二维数组中,并在二维字符串中从左到右和从上到下搜索类似“too”的子字符串

W E信用证O/n M E T O Z/n O H O C O/n R P O R A/n T I O N
这里的/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.

共 (2) 个答案

  1. # 1 楼答案

    出现错误是因为当您试图访问索引2之外的元素时,字符串TOO的数组中的最大索引b2

    按如下方式操作:

    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
    
  2. # 2 楼答案

                    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