Can I create a multidimensional array using single arrays to fill?











up vote
1
down vote

favorite












Say I had two randomly generated arrays (rows and columns) and I want them to be used to fill a multidimensional array. With the arrays being rows and columns, the multidimensional array would be matrix[rows][columns]. Just an example of how I generated rows:



int rows = new int[20];         
for(int i = 0; i < rows.length; i++) {
rows[i] = (int)(Math.random()*1000 + 1);
}
System.out.println("Rows Generated: " + Arrays.toString(randomArrayN));
}
}


The columns array was also built this way. Is there an easy way to create a two-dimension array and use these arrays to fill them?
I know this syntax is wrong but basically I want to know if there is a way to do this:



int matrix = new int[rows][columns];









share|improve this question


















  • 1




    What is the randomArrayN in your first snippet?
    – mettleap
    Nov 9 at 16:04






  • 4




    How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
    – Glains
    Nov 9 at 16:06






  • 1




    If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
    – mettleap
    Nov 9 at 16:11










  • whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
    – Benjamin Dare Edwards
    Nov 9 at 16:32












  • As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
    – Glains
    Nov 9 at 16:41

















up vote
1
down vote

favorite












Say I had two randomly generated arrays (rows and columns) and I want them to be used to fill a multidimensional array. With the arrays being rows and columns, the multidimensional array would be matrix[rows][columns]. Just an example of how I generated rows:



int rows = new int[20];         
for(int i = 0; i < rows.length; i++) {
rows[i] = (int)(Math.random()*1000 + 1);
}
System.out.println("Rows Generated: " + Arrays.toString(randomArrayN));
}
}


The columns array was also built this way. Is there an easy way to create a two-dimension array and use these arrays to fill them?
I know this syntax is wrong but basically I want to know if there is a way to do this:



int matrix = new int[rows][columns];









share|improve this question


















  • 1




    What is the randomArrayN in your first snippet?
    – mettleap
    Nov 9 at 16:04






  • 4




    How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
    – Glains
    Nov 9 at 16:06






  • 1




    If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
    – mettleap
    Nov 9 at 16:11










  • whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
    – Benjamin Dare Edwards
    Nov 9 at 16:32












  • As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
    – Glains
    Nov 9 at 16:41















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Say I had two randomly generated arrays (rows and columns) and I want them to be used to fill a multidimensional array. With the arrays being rows and columns, the multidimensional array would be matrix[rows][columns]. Just an example of how I generated rows:



int rows = new int[20];         
for(int i = 0; i < rows.length; i++) {
rows[i] = (int)(Math.random()*1000 + 1);
}
System.out.println("Rows Generated: " + Arrays.toString(randomArrayN));
}
}


The columns array was also built this way. Is there an easy way to create a two-dimension array and use these arrays to fill them?
I know this syntax is wrong but basically I want to know if there is a way to do this:



int matrix = new int[rows][columns];









share|improve this question













Say I had two randomly generated arrays (rows and columns) and I want them to be used to fill a multidimensional array. With the arrays being rows and columns, the multidimensional array would be matrix[rows][columns]. Just an example of how I generated rows:



int rows = new int[20];         
for(int i = 0; i < rows.length; i++) {
rows[i] = (int)(Math.random()*1000 + 1);
}
System.out.println("Rows Generated: " + Arrays.toString(randomArrayN));
}
}


The columns array was also built this way. Is there an easy way to create a two-dimension array and use these arrays to fill them?
I know this syntax is wrong but basically I want to know if there is a way to do this:



int matrix = new int[rows][columns];






java arrays multidimensional-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 15:59









Benjamin Dare Edwards

819




819








  • 1




    What is the randomArrayN in your first snippet?
    – mettleap
    Nov 9 at 16:04






  • 4




    How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
    – Glains
    Nov 9 at 16:06






  • 1




    If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
    – mettleap
    Nov 9 at 16:11










  • whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
    – Benjamin Dare Edwards
    Nov 9 at 16:32












  • As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
    – Glains
    Nov 9 at 16:41
















  • 1




    What is the randomArrayN in your first snippet?
    – mettleap
    Nov 9 at 16:04






  • 4




    How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
    – Glains
    Nov 9 at 16:06






  • 1




    If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
    – mettleap
    Nov 9 at 16:11










  • whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
    – Benjamin Dare Edwards
    Nov 9 at 16:32












  • As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
    – Glains
    Nov 9 at 16:41










1




1




What is the randomArrayN in your first snippet?
– mettleap
Nov 9 at 16:04




What is the randomArrayN in your first snippet?
– mettleap
Nov 9 at 16:04




4




4




How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
– Glains
Nov 9 at 16:06




How exactly should the matrix be filled from your rows and columns array? Considering you would have n rows and m columns you have n + m values, but the matrix size is n * m.
– Glains
Nov 9 at 16:06




1




1




If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
– mettleap
Nov 9 at 16:11




If you try and add rows and columns at the same time to a 2d array, it would result in many conflicts ... so it depends on how you want to construct the multi-dimensional array as @Glains has pointed out
– mettleap
Nov 9 at 16:11












whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
– Benjamin Dare Edwards
Nov 9 at 16:32






whoops, dont mind the randomArrayN, it was meant to be rows. I had changed the name of the array from that to rows. As for @Glains , the reality likely is that it is just more simple to create the values of rows and columns within the two-dimensional array rather than try to fill it with arrays already made, then?
– Benjamin Dare Edwards
Nov 9 at 16:32














As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
– Glains
Nov 9 at 16:41






As explained above, consider you have rows = {1, 2, 3} and columns = {3, 4, 6}. If you got matrix = [rows.length][columns.length] you have only 6 values, but the matrix has a size of 3 * 3 = 9. How do you want to calculate the values from your rows and column?
– Glains
Nov 9 at 16:41














3 Answers
3






active

oldest

votes

















up vote
1
down vote













If you need a 2d array filled with random ints, you can use a nested loop similar to what you used to genrate your rows above:



public static void main(String args) {
int matrix = new int[20][20];
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[i].length; j++){
matrix[i][j] = (int)(Math.random()*1000 + 1);
}
}

for(int row : matrix){
System.out.println(Arrays.toString(row));
}
}





share|improve this answer




























    up vote
    0
    down vote













    Yea you definitely can, though it may be a bit more confusing.



    You can initalize the 3d array with this:



    // test is a 3d array
    int test = {
    {
    {1, -2, 3},
    {2, 3, 4}
    },
    {
    {-4, -5, 6, 9},
    {1},
    {2, 3}
    }
    };


    I like to think this in terms or 3d obects, like a regtangluar prism. Each row (height)on the prism hold 2 things Length and Width, or a x,y. in other worlds you have a 2d array, one value is the column, the other is the row, in an array making it 3d.



    enter image description here



    now what if you want to print out values of the 3d array?
    simply do this :



     for (int array2D: test) {
    for (int array1D: array2D) {
    for(int item: array1D) {
    System.out.println(item);
    }
    }
    }





    share|improve this answer




























      up vote
      0
      down vote













      Although it's convenient to think of a 2-dimensional array in terms of rows and columns, a 2-dimensional array is just an array of arrays.



      For example,



          int values1 = {1, 2 ,3};
      int matrix1 = {values1, values1, values1};

      matrix1[1][1] = 42;
      System.out.println(matrix1[0][1]);
      System.out.println(matrix1[1][1]);
      System.out.println(matrix1[2][1]);


      or



          int values2 = {1, 2 ,3};
      int matrix2 = new int[3][3];
      matrix2[0] = values2;
      matrix2[1] = values2;
      matrix2[2] = values2;

      matrix2[1][1] = 42;
      System.out.println(matrix2[0][1]);
      System.out.println(matrix2[1][1]);
      System.out.println(matrix2[2][1]);


      Note that in both cases a values array is used multiple times to initialize each row of the matrix. So any change to any matrix element is reflected in the other rows (since each row is actually the same array). There may be cases where this is useful. If the elements need to be independent then use something like this:



          int values3 = {1, 2 ,3};
      int matrix3 = new int[3][3];
      System.arraycopy(values3, 0, matrix3[0], 0, values3.length);
      System.arraycopy(values3, 0, matrix3[1], 0, values3.length);
      System.arraycopy(values3, 0, matrix3[2], 0, values3.length);

      matrix3[1][1] = 42;
      System.out.println(matrix3[0][1]);
      System.out.println(matrix3[1][1]);
      System.out.println(matrix3[2][1]);


      Each row of the matrix is initialized with a copy of the same value, but making changes to any element is independent of any other element (since it's a different array).



      Also see this other question if the matrix needs to be initialized element by element.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229189%2fcan-i-create-a-multidimensional-array-using-single-arrays-to-fill%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        If you need a 2d array filled with random ints, you can use a nested loop similar to what you used to genrate your rows above:



        public static void main(String args) {
        int matrix = new int[20][20];
        for(int i = 0; i < matrix.length; i++){
        for(int j = 0; j < matrix[i].length; j++){
        matrix[i][j] = (int)(Math.random()*1000 + 1);
        }
        }

        for(int row : matrix){
        System.out.println(Arrays.toString(row));
        }
        }





        share|improve this answer

























          up vote
          1
          down vote













          If you need a 2d array filled with random ints, you can use a nested loop similar to what you used to genrate your rows above:



          public static void main(String args) {
          int matrix = new int[20][20];
          for(int i = 0; i < matrix.length; i++){
          for(int j = 0; j < matrix[i].length; j++){
          matrix[i][j] = (int)(Math.random()*1000 + 1);
          }
          }

          for(int row : matrix){
          System.out.println(Arrays.toString(row));
          }
          }





          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            If you need a 2d array filled with random ints, you can use a nested loop similar to what you used to genrate your rows above:



            public static void main(String args) {
            int matrix = new int[20][20];
            for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
            matrix[i][j] = (int)(Math.random()*1000 + 1);
            }
            }

            for(int row : matrix){
            System.out.println(Arrays.toString(row));
            }
            }





            share|improve this answer












            If you need a 2d array filled with random ints, you can use a nested loop similar to what you used to genrate your rows above:



            public static void main(String args) {
            int matrix = new int[20][20];
            for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
            matrix[i][j] = (int)(Math.random()*1000 + 1);
            }
            }

            for(int row : matrix){
            System.out.println(Arrays.toString(row));
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 16:39









            Eritrean

            3,2091814




            3,2091814
























                up vote
                0
                down vote













                Yea you definitely can, though it may be a bit more confusing.



                You can initalize the 3d array with this:



                // test is a 3d array
                int test = {
                {
                {1, -2, 3},
                {2, 3, 4}
                },
                {
                {-4, -5, 6, 9},
                {1},
                {2, 3}
                }
                };


                I like to think this in terms or 3d obects, like a regtangluar prism. Each row (height)on the prism hold 2 things Length and Width, or a x,y. in other worlds you have a 2d array, one value is the column, the other is the row, in an array making it 3d.



                enter image description here



                now what if you want to print out values of the 3d array?
                simply do this :



                 for (int array2D: test) {
                for (int array1D: array2D) {
                for(int item: array1D) {
                System.out.println(item);
                }
                }
                }





                share|improve this answer

























                  up vote
                  0
                  down vote













                  Yea you definitely can, though it may be a bit more confusing.



                  You can initalize the 3d array with this:



                  // test is a 3d array
                  int test = {
                  {
                  {1, -2, 3},
                  {2, 3, 4}
                  },
                  {
                  {-4, -5, 6, 9},
                  {1},
                  {2, 3}
                  }
                  };


                  I like to think this in terms or 3d obects, like a regtangluar prism. Each row (height)on the prism hold 2 things Length and Width, or a x,y. in other worlds you have a 2d array, one value is the column, the other is the row, in an array making it 3d.



                  enter image description here



                  now what if you want to print out values of the 3d array?
                  simply do this :



                   for (int array2D: test) {
                  for (int array1D: array2D) {
                  for(int item: array1D) {
                  System.out.println(item);
                  }
                  }
                  }





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Yea you definitely can, though it may be a bit more confusing.



                    You can initalize the 3d array with this:



                    // test is a 3d array
                    int test = {
                    {
                    {1, -2, 3},
                    {2, 3, 4}
                    },
                    {
                    {-4, -5, 6, 9},
                    {1},
                    {2, 3}
                    }
                    };


                    I like to think this in terms or 3d obects, like a regtangluar prism. Each row (height)on the prism hold 2 things Length and Width, or a x,y. in other worlds you have a 2d array, one value is the column, the other is the row, in an array making it 3d.



                    enter image description here



                    now what if you want to print out values of the 3d array?
                    simply do this :



                     for (int array2D: test) {
                    for (int array1D: array2D) {
                    for(int item: array1D) {
                    System.out.println(item);
                    }
                    }
                    }





                    share|improve this answer












                    Yea you definitely can, though it may be a bit more confusing.



                    You can initalize the 3d array with this:



                    // test is a 3d array
                    int test = {
                    {
                    {1, -2, 3},
                    {2, 3, 4}
                    },
                    {
                    {-4, -5, 6, 9},
                    {1},
                    {2, 3}
                    }
                    };


                    I like to think this in terms or 3d obects, like a regtangluar prism. Each row (height)on the prism hold 2 things Length and Width, or a x,y. in other worlds you have a 2d array, one value is the column, the other is the row, in an array making it 3d.



                    enter image description here



                    now what if you want to print out values of the 3d array?
                    simply do this :



                     for (int array2D: test) {
                    for (int array1D: array2D) {
                    for(int item: array1D) {
                    System.out.println(item);
                    }
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 16:08









                    joshau

                    748




                    748






















                        up vote
                        0
                        down vote













                        Although it's convenient to think of a 2-dimensional array in terms of rows and columns, a 2-dimensional array is just an array of arrays.



                        For example,



                            int values1 = {1, 2 ,3};
                        int matrix1 = {values1, values1, values1};

                        matrix1[1][1] = 42;
                        System.out.println(matrix1[0][1]);
                        System.out.println(matrix1[1][1]);
                        System.out.println(matrix1[2][1]);


                        or



                            int values2 = {1, 2 ,3};
                        int matrix2 = new int[3][3];
                        matrix2[0] = values2;
                        matrix2[1] = values2;
                        matrix2[2] = values2;

                        matrix2[1][1] = 42;
                        System.out.println(matrix2[0][1]);
                        System.out.println(matrix2[1][1]);
                        System.out.println(matrix2[2][1]);


                        Note that in both cases a values array is used multiple times to initialize each row of the matrix. So any change to any matrix element is reflected in the other rows (since each row is actually the same array). There may be cases where this is useful. If the elements need to be independent then use something like this:



                            int values3 = {1, 2 ,3};
                        int matrix3 = new int[3][3];
                        System.arraycopy(values3, 0, matrix3[0], 0, values3.length);
                        System.arraycopy(values3, 0, matrix3[1], 0, values3.length);
                        System.arraycopy(values3, 0, matrix3[2], 0, values3.length);

                        matrix3[1][1] = 42;
                        System.out.println(matrix3[0][1]);
                        System.out.println(matrix3[1][1]);
                        System.out.println(matrix3[2][1]);


                        Each row of the matrix is initialized with a copy of the same value, but making changes to any element is independent of any other element (since it's a different array).



                        Also see this other question if the matrix needs to be initialized element by element.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Although it's convenient to think of a 2-dimensional array in terms of rows and columns, a 2-dimensional array is just an array of arrays.



                          For example,



                              int values1 = {1, 2 ,3};
                          int matrix1 = {values1, values1, values1};

                          matrix1[1][1] = 42;
                          System.out.println(matrix1[0][1]);
                          System.out.println(matrix1[1][1]);
                          System.out.println(matrix1[2][1]);


                          or



                              int values2 = {1, 2 ,3};
                          int matrix2 = new int[3][3];
                          matrix2[0] = values2;
                          matrix2[1] = values2;
                          matrix2[2] = values2;

                          matrix2[1][1] = 42;
                          System.out.println(matrix2[0][1]);
                          System.out.println(matrix2[1][1]);
                          System.out.println(matrix2[2][1]);


                          Note that in both cases a values array is used multiple times to initialize each row of the matrix. So any change to any matrix element is reflected in the other rows (since each row is actually the same array). There may be cases where this is useful. If the elements need to be independent then use something like this:



                              int values3 = {1, 2 ,3};
                          int matrix3 = new int[3][3];
                          System.arraycopy(values3, 0, matrix3[0], 0, values3.length);
                          System.arraycopy(values3, 0, matrix3[1], 0, values3.length);
                          System.arraycopy(values3, 0, matrix3[2], 0, values3.length);

                          matrix3[1][1] = 42;
                          System.out.println(matrix3[0][1]);
                          System.out.println(matrix3[1][1]);
                          System.out.println(matrix3[2][1]);


                          Each row of the matrix is initialized with a copy of the same value, but making changes to any element is independent of any other element (since it's a different array).



                          Also see this other question if the matrix needs to be initialized element by element.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Although it's convenient to think of a 2-dimensional array in terms of rows and columns, a 2-dimensional array is just an array of arrays.



                            For example,



                                int values1 = {1, 2 ,3};
                            int matrix1 = {values1, values1, values1};

                            matrix1[1][1] = 42;
                            System.out.println(matrix1[0][1]);
                            System.out.println(matrix1[1][1]);
                            System.out.println(matrix1[2][1]);


                            or



                                int values2 = {1, 2 ,3};
                            int matrix2 = new int[3][3];
                            matrix2[0] = values2;
                            matrix2[1] = values2;
                            matrix2[2] = values2;

                            matrix2[1][1] = 42;
                            System.out.println(matrix2[0][1]);
                            System.out.println(matrix2[1][1]);
                            System.out.println(matrix2[2][1]);


                            Note that in both cases a values array is used multiple times to initialize each row of the matrix. So any change to any matrix element is reflected in the other rows (since each row is actually the same array). There may be cases where this is useful. If the elements need to be independent then use something like this:



                                int values3 = {1, 2 ,3};
                            int matrix3 = new int[3][3];
                            System.arraycopy(values3, 0, matrix3[0], 0, values3.length);
                            System.arraycopy(values3, 0, matrix3[1], 0, values3.length);
                            System.arraycopy(values3, 0, matrix3[2], 0, values3.length);

                            matrix3[1][1] = 42;
                            System.out.println(matrix3[0][1]);
                            System.out.println(matrix3[1][1]);
                            System.out.println(matrix3[2][1]);


                            Each row of the matrix is initialized with a copy of the same value, but making changes to any element is independent of any other element (since it's a different array).



                            Also see this other question if the matrix needs to be initialized element by element.






                            share|improve this answer












                            Although it's convenient to think of a 2-dimensional array in terms of rows and columns, a 2-dimensional array is just an array of arrays.



                            For example,



                                int values1 = {1, 2 ,3};
                            int matrix1 = {values1, values1, values1};

                            matrix1[1][1] = 42;
                            System.out.println(matrix1[0][1]);
                            System.out.println(matrix1[1][1]);
                            System.out.println(matrix1[2][1]);


                            or



                                int values2 = {1, 2 ,3};
                            int matrix2 = new int[3][3];
                            matrix2[0] = values2;
                            matrix2[1] = values2;
                            matrix2[2] = values2;

                            matrix2[1][1] = 42;
                            System.out.println(matrix2[0][1]);
                            System.out.println(matrix2[1][1]);
                            System.out.println(matrix2[2][1]);


                            Note that in both cases a values array is used multiple times to initialize each row of the matrix. So any change to any matrix element is reflected in the other rows (since each row is actually the same array). There may be cases where this is useful. If the elements need to be independent then use something like this:



                                int values3 = {1, 2 ,3};
                            int matrix3 = new int[3][3];
                            System.arraycopy(values3, 0, matrix3[0], 0, values3.length);
                            System.arraycopy(values3, 0, matrix3[1], 0, values3.length);
                            System.arraycopy(values3, 0, matrix3[2], 0, values3.length);

                            matrix3[1][1] = 42;
                            System.out.println(matrix3[0][1]);
                            System.out.println(matrix3[1][1]);
                            System.out.println(matrix3[2][1]);


                            Each row of the matrix is initialized with a copy of the same value, but making changes to any element is independent of any other element (since it's a different array).



                            Also see this other question if the matrix needs to be initialized element by element.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 9 at 16:35









                            Andrew S

                            1,4721510




                            1,4721510






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229189%2fcan-i-create-a-multidimensional-array-using-single-arrays-to-fill%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()