简体   繁体   English

数组在Java中越界越好?

[英]Array out of bounds in Java?

I'm trying to run this code but I keep getting an out of bounds error. 我正在尝试运行此代码,但我一直越界错误。 This is just the sub class for the super class "Simpler." 这只是超类“ Simpler”的子类。 The user enters in a string then the string is broken down into a char array. 用户输入一个字符串,然后将该字符串分解为一个char数组。 The array should not be smaller than the string yet I am getting this error. 该数组不应小于字符串,但我收到此错误。 Can you tell me what I'm doing wrong? 你能告诉我我做错了吗? Thanks! 谢谢!

import java.util.*;
public class Encrypt extends Simpler
{
    public void encryption()
    {
        boolean loop = true;
        while(loop==true)
        {
            Scanner scan = new Scanner(System.in);

            System.out.println("Please enter the phrase you'd like to encrypt: ");
            String inPhrase = scan.nextLine();
            char[] chars = inPhrase.toCharArray();
            char tempArray[] = new char[chars.length+1];
            tempArray = chars;
            chars = new char[tempArray.length];
            for (int i = inPhrase.length(); i<inPhrase.length(); i--)
            {
                if(chars[i]=='a')
                {
                    chars[i]='1';
                }
                else if(chars[i]=='b')
                {
                    chars[i]='2';
                }
                else if(chars[i]=='c')
                {
                    chars[i]='3';
                }
                else if(chars[i]=='d')
                {
                    chars[i]='4';
                }
                else if(chars[i]=='z')//I skipped some lines here for convienence
                {
                    chars[i]='{';
                }
                else if(chars[i]==' ')
                {
                    chars[i]='}';
                }
            }
            String outPhrase = new String(chars);
            System.out.println(outPhrase);
        }
    }
}

I think your for loop statement should look like this: 我认为您的for循环语句应如下所示:

for (int i = 0; i < inPhrase.length(); i++)

if you're counting up, and like this: 如果您要数的话,例如:

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

if you're counting down. 如果您要倒数。

update 更新

On looking back over it, I think there is more to it than that though. 回顾一下,我认为不仅限于此。 I think your code needs to be rewritten: 我认为您的代码需要重写:

String inPhrase = scan.nextLine();
char[] chars = inPhrase.toCharArray();
char tempArray[] = new char[chars.length()];

for (int i = 0; i < chars.length(); i++)
{
    if(chars[i]=='a')
    {
        tempArray[i]='1';
    }
.
.
.
.
}
String outPhrase = new String(tempArray);

No stop condition in the for loop, in this line: 在for循环中,在此行中没有停止条件:

for (int i = inPhrase.length(); i<inPhrase.length(); i--)

i gets 1, 0, -1, ... and wouldn't stop if -1 wouldn't throw an out of bounds exception i得到1, 0, -1, ...并且如果-1不会抛出界外异常也不会停止

在for循环中,只需将条件从i < inPhrase.length()更改为i >= 0就可以了。

First thing: 第一件事:

for (int i = inPhrase.length(); i<inPhrase.length(); i--)

you never enter your loop because you assign i = n & entry condition is i < n. 您永远不会进入循环,因为您分配了i = n且进入条件是i <n。

it should be 它应该是

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

Now, this also removes your arrayoutofbound exception because earlier, you tried to access chars[n] which is actually the n+1 th character of that array. 现在,这也删除了arrayoutofbound异常,因为先前您尝试访问chars [n],它实际上是该数组的第n + 1个字符。

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

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