Home > Software engineering >  Java add array in to array like a Python style
Java add array in to array like a Python style

Time:09-30

(first, sorry of my english, I am not native speaker, ehm, writer)

Hi, I am trying add array in to array...

code:

        int[] headArray = {16, 3, 2, 13,
                       5, 10, 11, 8,
                       9, 6, 7, 12,
                       4, 15, 14, 1};
        int[] miniArray = new int[4]; // helpArray
        int[][] array2D = new int[4][4];

        int count = 0;
        int countIndex = 0;

        for (int i = 0; i < headArray.length; i  ) {
            miniArray[count] = headArray[i];
            count  ;
            if (count == 4) {
                array2D[countIndex] = miniArray;
                count = 0;
                countIndex  ;
                // reset miniArray ? how? de-likn array? how?
            }
        }

when I visualize it, I see, 1st loop fill miniArray correctly, then, miniArray will be putted in to array2D to index 0 correctly, but in next step(s), all indexes of array2D are "linked" to last array lements of miniArray (visualization of work) after finish loop my array2D will be like:

array2D = {{4, 15, 14, 1},{4, 15, 14, 1},{4, 15, 14, 1},{4, 15, 14, 1}}

not what I expected:

array2D = {{16, 3, 2, 13},{5, 10, 11, 8},{9, 6, 7, 12},{4, 15, 14, 1}}

yes, of course, I know correct answer - it can be solved by:

        int k = 0;

        for (int i = 0; i < array2D.length; i  ) {
            for (int j = 0; j < array2D[0].length; j  ) {
                array2D[i][j] = headArray[k];
                k  ;
            }
        }

but it is problem solving like "I am looking at this problem by "row 0, cell 0, row 1, cell 0" etc..." but I am looking at this problem like "I have 16 stuffs on the table, I will grab 4 of them, put it in to miniBox and after that, put miniBox in to bigBox, then, another 4 stuffs put in to another miniBox and after that, put miniBox in to bigBox, etc etc" like a Python style:

headArray = [16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1]

count = 0

for i in range(16): # or range(len(headArray))
    miniArray.append(headArray[i])
    count  = 1
    if count == 4:
        array2D.append(miniArray)
        count = 0
        miniArray= []

why my Python-retranslated code to Java language does not work? How can I fix it?

Thank you very much. Marcel

CodePudding user response:

another 4 stuffs put in to another miniBox

This is what you need to do, but not what you are doing. You are putting the next 4 stuffs into the same miniBox.

To create a new miniBox, you need to create after the following line:

// reset miniArray ? how? de-likn array? how?
miniArray = new int[4];

So the complete loop would be this:

    for (int i = 0; i < headArray.length; i  ) {
        miniArray[count] = headArray[i];
        count  ;
        if (count == 4) {
            array2D[countIndex] = miniArray;
            count = 0;
            countIndex  ;
            // reset miniArray ? how? de-likn array? how?
            miniArray = new int[4];
        }
    }

CodePudding user response:

You have to create a new array for each row.

int[] headArray = { 16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1 };
int[][] array2D = new int[4][];

for (int i = 0, j = 0; i < headArray.length; i  = 4,   j)
    array2D[j] = Arrays.copyOfRange(headArray, i, i   4);

System.out.println(Arrays.deepToString(array2D));

output:

[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]
  • Related