【问题标题】:how to get a random value from one array to another in java如何在java中从一个数组到另一个数组中获取随机值
【发布时间】:2015-05-15 14:05:16
【问题描述】:

我想从char[] vowelchar[] consonant 中获取一个随机数组值到char[] firstNamechar[] lastName

我的 IDE (Eclipse) 没有显示

的错误

firstName[i]=consonant [random.nextInt(consonant.length)];

但是在运行代码时我会得到错误

`Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
0     at _01NameGenerator.main(_01NameGenerator.java:27)`

我该如何修正这个说法?

// http://www.javapractices.com/topic/TopicAction.do?Id=62
import java.util.Random;

public class _001NameGenerator {

    public static void main(String[] args) {
        Random random = new Random();

        int firstNameLength = 7; // fixed length, not "random"
        int lastNameLength = 5;  

        System.out.println("Your firstname will be "+firstNameLength+" and you lastname "+lastNameLength+" characters long.");

        char[] vowel = {'a', 'e', 'i', 'o', 'u', 'y'};      
        char[] consonant = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z'};

        char[] firstName = new char[firstNameLength];
        char[] lastName = new char[lastNameLength];


        boolean wechsel = true;

        for (int i = 0; i < firstNameLength; i++) {

            if (wechsel == true){

                firstName[i]=consonant [random.nextInt(20)];  // length of consonant array
                wechsel = false;

            } else {

                firstName[i]=vowel [random.nextInt(6)]; // length of vowel array
                wechsel = true;
            }

        }   

        for (int i = 0; i < lastNameLength; i++) {

            if (wechsel == true){

                lastName[i]=consonant [random.nextInt(20)];
                wechsel = false;

            } else {

                lastName[i]=vowel [random.nextInt(6)];
                wechsel = true;
            }

        }   

        System.out.println(firstName + "\n" + lastName);

    }

}

【问题讨论】:

  • 你认为char[] someArray = {}; 有什么作用,为什么你认为你可以以某种方式使用它someArray[0]?你知道数组有固定长度吗?
  • 嗨 pshemo,我不知道固定长度。我只是希望它被初始化。然而,不幸的是,给它firstNameLenght 的长度并不能解决问题。
  • 其实它解决了这个问题,但显然你还有一个。那么现在的错误信息是什么?
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at _01NameGenerator.main(_01NameGenerator.java:27)Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at _01NameGenerator.main(_01NameGenerator.java:32)
  • 这意味着您的循环正在尝试访问数组边界之外的元素。尝试适当限制[i][random.nextInt(consonant.length)] 的范围(使用适当数组的长度)。

标签: java arrays random indexoutofboundsexception


【解决方案1】:
char[] firstName = {};
char[] lastName = {};

这两行生成空列表,因此每当您尝试添加内容时,它都会给出ArrayIndexOutOfBoundsException: 0

既然你有上面的随机长度,试试这个:

char[] firstName = new char[firstNameLength];
char[] lastName = new char[lastNameLength];

此外,如果您事先不知道数组的长度,您可以使用 ArrayList,它是为这些目的而创建的。

【讨论】:

  • 谢谢。我用char[] firstName = new char[firstNameLength]; 尝试了你的建议。该数组现在具有正确的长度。但我得到同样的错误。我会更仔细地检查代码
  • @grerrg 我已经尝试过您编辑的主帖代码,现在它似乎可以正常工作了。唯一要添加的是打印行中字符串的字符数组。您可以通过将打印更改为 System.out.println(new String(firstName) + "\n" + new String(lastName)); Here is the ideone. 来做到这一点
【解决方案2】:

您已将 firstNamelastName 初始化为零长度数组。因此,在您的第一次迭代中,您尝试为 firstName[0] 分配一个值,但“0”是空数组的无效索引,因此出现异常。

你应该用长度初始化数组:

char[] firstName = new char[firstNameLength];
char[] lastName = new char[lastNameLength];

【讨论】:

    【解决方案3】:

    答案很明显:这两个数组都是零长度:

        char[] firstName = {};
        char[] lastName = {};
    

    【讨论】:

      【解决方案4】:

      您将 firstNamelastName 数组声明为空的零长度数组。您应该根据您的代码声明如下:

      char[] firstName = new char[firstNameLength];
      char[] lastName = new char[lastNameLength];
      

      这会阻止您获得 ArrayIndexOutOfBoundsException。

      【讨论】:

        猜你喜欢
        • 2018-10-04
        • 2018-05-15
        • 2019-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 2023-01-20
        相关资源
        最近更新 更多