-3

I have the following method that should return an array of 31 rows (with each row has 31 indexes). Here is my code:

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;
}

how to do this ? how to create arrayD that has two dimensions and pass 31 values (of arrayA) to a single row (of 31 indexes) every time arrayA changes (in summery, I want to store 'history' of arrayA in arrayD, so each row in D represents different states of A)?

McLan
  • 2,552
  • 9
  • 51
  • 85
  • 2
    And your code attempts to... – Luiggi Mendoza Apr 27 '13 at 15:14
  • It's really no clear what's going on, particularly as you're never doing anything with tempArray... – Jon Skeet Apr 27 '13 at 15:16
  • @LuiggiMendoza: the thing is: non of them was working, and I don't keep code that doesn't work, sorry (I understand our concern). however, after writing the question: I am wondering if there is a way to save my arrayA to a public variable as in c++ ? – McLan Apr 27 '13 at 15:18
  • 1
    In Java, the only way you can do that is by having an attribute of your class with type `int[]` (or the type of the array), then in your method modify this attribute as long as you wish. Otherwise, since [**Java method parameters are passed by value**](http://stackoverflow.com/q/40480/1065197), you would need to save the contents of the generated array in another array passed as parameter, i.e. `int[] arrayD (new parameter)... for(int i = 0; i < tempArray.length; i++) arrayD[i] = tempArray[i]`. – Luiggi Mendoza Apr 27 '13 at 15:23
  • 1
    Do you want to store 'history' of arrayA in arrayD? So each row in D represents different states of A? – gkiko Apr 27 '13 at 16:07
  • @Rania-Boy I'll search my projects. I've done this kind of thing – gkiko Apr 27 '13 at 16:18

1 Answers1

3

Make i public variable with initial value 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++;
}
gkiko
  • 2,283
  • 3
  • 30
  • 50
  • Do you initialize `arrayA` and `arrayD` with `new` before passing to this function? – gkiko Apr 27 '13 at 19:23
  • it works but both i and arrayD must be public variables .. thanks – McLan Apr 27 '13 at 19:35
  • 1
    With `i` not public, it would be harder to implement this method. Not sure about `arrayD` that it must be public. – gkiko Apr 27 '13 at 19:51