2015-05-15 38 views
-1

我想從char[] vowelchar[] consonant中獲得一個隨機數組值到char[] firstNamechar[] lastName如何從java中的一個數組中得到一個隨機值

運行代碼然而,當我的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); 

    } 

} 
+2

你覺得'char [] someArray = {};'確實如此,你爲什麼認爲你可以用'someArray [0]'的方式使用它?你知道陣列具有固定長度嗎? – Pshemo

+0

嗨pshemo,我不知道固定長度。我只是想讓它初始化。然而,給它'firstNameLenght'的長度不幸並不能解決問題。 – grerrg

+0

其實它解決了這個問題,但顯然你有另一個。那麼現在的錯誤信息是什麼? – Pshemo

回答

0

答案是顯而易見的:這兩個數組的長度爲0:

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

這兩條線產生空列表,所以每當你嘗試添加的東西它提供了一個ArrayIndexOutOfBoundsException: 0

既然你有上述隨機長度,嘗試這個代替:

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

此外,如果你就不會事先已知的陣列的長度,則可以使用一個ArrayList,其被用於創建這些目的。

+0

謝謝。我用'char [] firstName = new char [firstNameLength];'試過了你的建議。該陣列現在具有正確的長度。但我得到同樣的錯誤。我會更仔細地檢查代碼 – grerrg

+0

@grerrg我試過了你的主帖子的編輯代碼,現在它似乎沒有錯誤地工作。剩下的唯一要添加的就是打印行中的字符串char-array。你可以通過改變你的打印到'System.out.println(new String(firstName)+「\ n」+ new String(lastName));'[這裏是ideone。](http://ideone.com/cl26CS) –

1

您已將firstNamelastName初始化爲零長度數組。因此,在第一次迭代中,您嘗試爲firstName[0]分配一個值,但「0」是空數組的無效索引,因此是例外。

你應該長度初始化數組:

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

可以聲明firstNamelastName陣列作爲空零長度陣列。您應該根據您的代碼聲明如下:

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

這可以防止您獲取ArrayIndexOutOfBoundsException。

相關問題