简体   繁体   English

需要帮助来修复以下代码的arrayoutofboundary异常

[英]Need help to fix the arrayoutofboundary exception for the below code

How to fix arrayOutOfBoundException error message for the below code. 如何修复以下代码的arrayOutOfBoundException错误消息。 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. 当内部for循环中断时,j将超出数组范围。 add j-- before the line String output = String.valueOf(rev[j]); String output = String.valueOf(rev[j]);之前添加j--

On side note, you should make rev array big enough to hole whole string 在侧面说明,您应该使rev数组足够大,以使整个字符串通孔

  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 so

You initialized rev with size one less than the length of reverString 您初始化的rev的大小比reverString的长度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. 实际上,您对于嵌套for循环的整个想法是一团糟。 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. 要定义具有比acutual斯汀大小少一个长度的数组。

    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循环进行打印

    for (Character c : rev) System.out.print(c); 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); 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 . 您可以将结果保存在StringStringBuilder

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. 1.不需要两个循环来反转字符串,可以使用一个循环进行遍历并增加j的值来移动。

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. 其次,如果您注意到上述循环,您会发现它实际上被调用了6次,而i的值是(5,4,3,2,1,0),因此您需要一个大小为6的数组,这意味着大小rev的6应该是reverString.length()请参考下面的代码。

     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)); } 

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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