简体   繁体   中英

Need help to fix the arrayoutofboundary exception for the below code

How to fix arrayOutOfBoundException error message for the below code. This seem to be logically correct for me. But I'm not sure why I get this error. Need help on this

public class list  {

    public String reverseStr(String input){
        String reverString = input;
        int j=0;

        Character rev[] = new Character[reverString.length()-1];

        for(int i= reverString.length()-1;i> 0; --i){

            for(j = 0;j<= reverString.length()-1; ++j){
             rev[j] = reverString.charAt(i);
            }

        }
        String output = String.valueOf(rev[j]);
        return output;
    }

    public static void main(String args[]){
        String reverse = "Ambika";
        list li = new list();
        System.out.println("The reverse of " + reverse + " is " + li.reverseStr(reverse));
    }
}

When inner for loop breaks, j will reach beyond the array bounds. add j-- before the line String output = String.valueOf(rev[j]);

On side note, you should make rev array big enough to hole whole string

  Character rev[] = new Character[reverString.length()];
for(j = 0;j<= reverString.length()-1; ++j){

改成

 for(j = 0;j< reverString.length()-1; ++j){
class Untitled {

    public static String reverse( final String s )
    {
        return new StringBuilder(s).reverse().toString();            
    }

    public static void main(String[] args) {
        System.out.println( reverse( "Josh" ) );
    }
}

hsoJ

You initialized rev with size one less than the length of reverString

 Character rev[] = new Character[reverString.length()-1];

Change it to

Character rev[] = new Character[reverString.length()];

Actually, your whole idea of nested for-loop is a mess. You can simplify your program as follows;

public class list {

    public String reverseStr(String input) {
        String reverString = input;
        int j = 0;

        char rev[] = new char[reverString.length()];

        for (int i = reverString.length() - 1; i >= 0; --i) {

            rev[j++] = reverString.charAt(i);
        }

        String output = new String(rev);
        return output;
    }

    public static void main(String args[]) {
        String reverse = "Ambika";
        list li = new list();
        System.out.println("The reverse of " + reverse + " is "
                + li.reverseStr(reverse));
    }
}

There are so many issues in your source,

  1. You are defining an array with one length less than the acutual Sting size.

    Character rev[] = new Character[reverString.length()-1];

    It should have been,

    Character rev[] = new Character[reverString.length()-1];

  2. Printing the value using the Index

    String output = String.valueOf(rev[j]);

    You can print using for loop

    for (Character c : rev) System.out.print(c);

  3. You don't need two loops. One loop should be sufficient as below

    int j = 0; for (int i = reverString.length() - 1; i >= 0; --i) { rev[j] = reverString.charAt(i); j++; }

  4. Finally you don't need a separate array to hold your result. You can hold the result in a String or StringBuilder .

Couple of things I noticed in your program. 1. You do not need two loops to reverse the string, you can use one loop for traversing and increase the value of j to move.

int j=0;     
for(int i= reverString.length()-1;i>= 0; --i){           
             rev[j++] = reverString.charAt(i);
        }
  1. Second if you noticed the above loop you will find that it is actually being invoked 6 time for value of i which is ( 5,4,3,2,1,0), so you need a array of size 6 which mean the size of rev should be 6 which is reverString.length() Please refer the code below.

     public class List { public String reverseStr(String input){ String reverString = input; int j=0; char rev[] = new char[reverString.length()-1]; for(int i= reverString.length()-1;i>= 0; --i){ rev[j++] = reverString.charAt(i); } String output =String.valueOf(rev); return output; } public static void main(String args[]){ String reverse = "Ambika"; List li = new List(); System.out.println("The reverse of " + reverse + " is " + li.reverseStr(reverse)); } 

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM