2012-02-29 61 views
0

我想創建一個由5個索引組成的數組,索引與每個數組中的另一個整數組成索引。 5索引中的每個數組都需要如下所示的索引(10,100,1000,10000),但我不知道如何在我的for循環內填充這樣的數組,這通常與數組一起使用,而沒有它運行到無窮大,因爲我有 for(int x = 0; x < array.length; x ++), 我不能在這裏使用x變量; int [x < - (syntax error)] array = {ten = new int [arraySize],hundred = new int [arraySize],thousand = new int [arraySize],tenthousand = new int [arraySize], without it告訴我有一個語法錯誤。我不知道該怎麼辦。我需要知道如何用其他數組填充數組索引

所有這些代碼都是自己類的方法的一部分,如果這有助於更好地理解。

公衆詮釋ArrayArray(INT ARRAYSIZE,詮釋randomNumber){

arraySizes = arraySize; 

    for(int x = 0; x < array.length; x++) { 
     size = 0; 

    Random gen = new Random(randomNumber); 

    int[]ten; 
    ten = new int[arraySize]; 
    int[] hundred; 
    hundred = new int[arraySize]; 
    int[]thousand; 
    thousand = new int[arraySize]; 
    int[]tenThousand; 
    tenThousand = new int[arraySize]; 

    int[] array = {ten[x] = gen.nextInt(10), hundred[x] = gen.nextInt(100), 
      thousand[x] = gen.nextInt(1000), tenThousand[x] = gen.nextInt(10000)}; 

    return array[]; 
    } 

這改變了我的問題有點我認爲香港專業教育學院在其上工作過之後,得到了它。這看起來像它會返回我想要它做什麼?即時通訊將有一個驅動程序,我將調用此方法給定的數組大小和給定數量的隨機整數。

+0

你能編輯你的文章,並把你有問題的代碼? – moesef 2012-02-29 02:34:26

+1

是的。發佈你的代碼,我很難處理你的問題。 – Marl 2012-02-29 02:47:29

+0

這種方法的回報並不能順利工作 – Tigh 2012-02-29 02:51:37

回答

0

如果你試圖返回和數組的功能試試這個:

public int[][] ArrayArray(int arraySize, int randomNumber) { 
    Random gen = new Random(randomNumber); 
    int[]ten= new int[10]; 
    int[] hundred= new int[100]; 
    int[]thousand= new int[1000]; 
    int[]tenThousand= new int[10000];  
    int[][] array = {ten,hundred,thousand,tenthousand}; 
    return array; 
} 

通知是int[][]而不是int[]。這是因爲你試圖返回一個二維數組而不僅僅是一個單一的數組。

讓我知道這是如何工作的。

+0

我擺脫了arraySize參數,因爲我可以在方法中說一個給定數組的大小將會是什麼。 INT [] 10; \t \t ten = new int [10]; 我還需要創建多陣列嗎? – Tigh 2012-02-29 03:12:43

+0

是的,否則你會得到一堆相同大小的數組。但你想用這些數組做什麼?你想用隨機數填充它們嗎? – moesef 2012-02-29 03:13:33

+0

我現在正在做類中的排序方法,我的任務是取這些特定的數組,並用排序方法(降序,遞增順序和3個隨機生成列表)以五種不同的方式排列它們中的每一個。即時通訊不知道如何正確的方式。 – Tigh 2012-02-29 03:19:16

0

如果您正在嘗試創建數組數組,那麼多維數組就是要走的路。

int arraySize = 10; 

int[]ten = new int[arraySize]; 
int[] hundred = new int[arraySize]; 
int[]thousand = new int[arraySize]; 
int[]tenThousand = new int[arraySize]; 

int[][] array = new int[4][arraySize]; 

array[0] = ten; 
array[1] = hundred; 
array[2] = thousand; 
array[3] = tenThousand; 

另外傳遞2維數組與數組相同。例如。

public static int hello(int[][] pass) { 
    return pass; 
}