2014-03-30 52 views
0

在這裏我做了一個代碼,但單個數組,但現在我想將其轉換爲雙數組[] []。如何將單個數組轉換爲雙數組

有些事情應該改變,但我可以得到它的工作。

這是獲得我想要更改爲雙數組的單個數組的類的一部分。

public House[][] neighbors(House victim) { 
    House[][] n = new House[8]; 

    int row = victim.address/size; 
    int col = victim.address % size; 

    if (row != 0 && row != (size - 1) && col != 0 && col != (size - 1)) { 
     n[0] = houses[victim.address - 1]; 
     n[1] = houses[victim.address + 1]; 
     n[2] = houses[victim.address - size]; 
     n[3] = houses[victim.address - size - 1]; 
     n[4] = houses[victim.address - size + 1]; 
     n[5] = houses[victim.address + size]; 
     n[6] = houses[victim.address + size - 1]; 
     n[7] = houses[victim.address + size + 1]; 
     return n; 
    } 

    if (row == 0 && col != 0 && col != (size - 1)) { 
     n[0] = houses[victim.address - 1]; 
     n[1] = houses[victim.address + 1]; 
     n[2] = houses[victim.address + size]; 
     n[3] = houses[victim.address + size - 1]; 
     n[4] = houses[victim.address + size + 1]; 
     n[5] = houses[victim.address + (size * (size - 1))]; 
     n[6] = houses[victim.address + (size * (size - 1)) + 1]; 
     n[7] = houses[victim.address + (size * (size - 1)) - 1]; 
     return n; 
    } 

    if (row == (size - 1) && col != 0 && col != (size - 1)) { 
     n[0] = houses[victim.address - 1]; 
     n[1] = houses[victim.address + 1]; 
     n[2] = houses[victim.address - (size * (size - 1))]; 
     n[3] = houses[victim.address - (size * (size - 1)) + 1]; 
     n[4] = houses[victim.address - (size * (size - 1)) - 1]; 
     n[5] = houses[victim.address - size]; 
     n[6] = houses[victim.address - size - 1]; 
     n[7] = houses[victim.address - size + 1]; 
     return n; 
    } 

    if (col == 0) { 
     n[0] = houses[victim.address + (size - 1)]; 
     n[1] = houses[victim.address + 1]; 
     n[2] = houses[victim.address - size]; 
     n[3] = houses[victim.address - size - 1]; 
     n[4] = houses[victim.address - size + 1]; 
     n[5] = houses[victim.address - (size * (size - 1))]; 
     n[6] = houses[victim.address - (size * (size - 1)) + 1]; 
     n[7] = houses[victim.address - (size * (size - 1)) - 1]; 
     return n; 
    } 
+0

什麼是「大小」? – ray

回答

0

我假設,通過「double」數組,你的意思是你想要一個2D數組。既然你沒有指定你想要的double數組的尺寸,我將假設這個例子爲2 x 4

而是寫

House[][] n = new House[8]; 

你想

House[][] n = new House[2][4]; 

然後,你要替換以下形式的每一行:

n[0] = houses[victim.address - 1] 

與以下形式的行:

n[0][1] = houses[victim.address - 1] 

第一個索引指定行號,第二個索引指定您的二維數組的列號。您可能比我更瞭解哪些索引對應與哪個特定房屋匹配。

+0

對於這部分:n [0] [1] =房屋[victim.address - 1] 我是這樣做的,但它說:不能從House []轉換到House。 – user3477300