-1

The next code creates a random multidimensional array, named 'arista', and fills each of the slots of the md-array with integers.

Then, it prints it. And you can see how are the slots filled with integers. Then I tried to create an array, called 'lista', which should be a list containing ALL the values stored in the multidimensional array, by typing:

System.out.printf(Arrays.toString(lista)); 

But the result is not what I expected. Only the last row of 'arista' appears in the array 'lista' and the other portion of the slots of the array 'lista' are zeroes.

How could I correct this? what is wrong?

The complete code is:

public static void main(String[] args) {

    int renglones = (int) (Math.random() * 5) + 5;
    int columnas = (int) (Math.random() * 5) + 5;

    int[][] arista = new int[renglones][columnas];

    int[] lista;
    lista = new int[renglones * columnas];
    int k = 1;  

    for (int i=0; i < renglones; i++ ){
        for (int j=0; j < columnas; j++) {

        arista[i][j] = k++;
        lista[j] = arista[i][j];

        }

    }

        for (int i = 0; i < renglones; i++) {

        for (int j = 0; j < columnas; j++) {
            System.out.printf("[%d][%d] = %d \n", i, j, arista[i][j]);
        }
        System.out.println();
    }

        System.out.printf(Arrays.toString(lista)); 


}
Trux
  • 157
  • 6
  • use a debugger and go through it. – Ousmane D. Dec 14 '17 at 22:45
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Ousmane D. Dec 14 '17 at 22:45
  • 1
    Your re-use of `j` as the index for `lista` is incorrect. Think about how that index should change as both `j` _and_ `i` change. – rgettman Dec 14 '17 at 22:46
  • I have downvoted this question because there is no evidence of any debugging performed on this code. Please [edit] your question to show us what your debugging has uncovered, as well as a specific question about a specific line of code. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Joe C Dec 14 '17 at 22:48
  • 1
    @Aominè While I agree the OP needs to spend some time in a debugger, the question being asked has nothing to do with debuggers, and thus the duplicate vote is inappropriate. Other close vote reasons are available and more appropriate for this question. – Joe C Dec 14 '17 at 22:50
  • @rgettman I thought I should change to `lista[i*j] = arista[i][j];` but that doesn't work very well either – Trux Dec 14 '17 at 23:19
  • @JoeC I am learning how to debug, and probably will go through it. My example is a minimal, an verifiable example, though. The specific line of codes in which I have doubts are, are `System.out.printf(Arrays.toString(lista)); ` and `lista[i*j] = arista[i][j];` I dont care about the downvotes BTW – Trux Dec 14 '17 at 23:46

1 Answers1

0

Your re-use of j as the index for lista is incorrect. When j starts over at 0, it will overwrite the contents of the first j entries, leaving only the last row at the end.

Multiplying i by j won't work either; when either of them are 0, then you will overwrite the first entry, because your calculated index will be 0.

When i goes from 0 to 1, you want to start at an index number past the first columnas entries, not 0. When i goes from 1 to 2, you want start at index 2*columnas.

    -- 1st row --               -- 2nd row --
[0][1]...[columnas - 1] [columnas][columnas + 1] ... [2*columnas - 1] ...

Multiply i by columnas then add j to get your index.

lista[i*columnas + j] = arista[i][j];
rgettman
  • 176,041
  • 30
  • 275
  • 357