2013-04-27 63 views
-3

我有以下方法应该返回一个31行的数组(每行有31个索引)。这里是我的代码:如何用增量行和固定数量的索引创建一个数组

public static int[] funcA (int[] arrayB,int[] arrayC){ 
    int d,i; 
    int [] arrayA = new int[31]; 
    int [] arrayD= new int[31]; 

    for (d=0; d<31; d++){ 
     int []temp = new int[31]; 
     for (i=0; i<31; i++){ 
      arrayA [i] = arrayB[i]^arrayC[i]; 
      } 
     // at this point, 31 values are generated inside the arrayA. 
     // my problem is here: the 31 values of arrayA should be assigned to arrayD in row[d](the row has 31 indexes) 
     // then, an operation occur to arrayC and the values of arrayA are changed   
    } 
    return arrayD; 
} 

如何做到这一点?如何创建具有两个维度的arrayD,并在每次arrayA发生变化时(将数组A的arrayA的历史记录存储在arrayD中,因此每个数据行都在D中)将31个值(arrayA)传递给一行(31个索引)代表A)的不同状态?

+2

而且你的代码试图... – 2013-04-27 15:14:51

+0

这真的没有明确发生了什么事情,特别是你永远做什么用tempArray ... – 2013-04-27 15:16:22

+0

@ LuiggiMendoza:事情是:非他们工作,我不保留代码不起作用,对不起(我明白我们的关注)。然而,在写这个问题后:我想知道是否有一种方法可以将我的arrayA保存为一个公共变量,就像在C++中一样? – 2013-04-27 15:18:30

回答

3

i公共变量与初始值0

private void copy(int[] arrayA, int[][] arrayD) { 
    int copySize = arrayA.length; 
    System.arraycopy(arrayA, 0, arrayD[i], 0, copySize); 
    // i should increment every time the 'history' is stored in arrayD 
    i++; 
} 
+0

你是否在传递给这个函数之前用'new'初始化'arrayA'和'arrayD'? – gkiko 2013-04-27 19:23:34

+0

它的工作原理,但I和arrayD必须是公共变量..感谢 – 2013-04-27 19:35:07

+1

随着'i'不公开,这将是难以实现此方法。不确定'arrayD'它必须是公开的。 – gkiko 2013-04-27 19:51:30