Auto-generate variable size 2D array from 1D arrays












0














I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !



public class c {
public int f;

public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}

public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}









share|improve this question
























  • You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
    – Hrudayanath
    Nov 12 '18 at 7:07










  • Possible duplicate of How to convert a 1d array to 2d array?
    – LuCio
    Nov 12 '18 at 8:42
















0














I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !



public class c {
public int f;

public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}

public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}









share|improve this question
























  • You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
    – Hrudayanath
    Nov 12 '18 at 7:07










  • Possible duplicate of How to convert a 1d array to 2d array?
    – LuCio
    Nov 12 '18 at 8:42














0












0








0







I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !



public class c {
public int f;

public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}

public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}









share|improve this question















I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !



public class c {
public int f;

public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}

public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}






java arrays multidimensional-array methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 7:18









Deedar Ali Brohi

21519




21519










asked Nov 12 '18 at 6:57









Sam D

1




1












  • You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
    – Hrudayanath
    Nov 12 '18 at 7:07










  • Possible duplicate of How to convert a 1d array to 2d array?
    – LuCio
    Nov 12 '18 at 8:42


















  • You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
    – Hrudayanath
    Nov 12 '18 at 7:07










  • Possible duplicate of How to convert a 1d array to 2d array?
    – LuCio
    Nov 12 '18 at 8:42
















You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07




You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07












Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42




Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42












2 Answers
2






active

oldest

votes


















0














This can be achieved using stream api in java



int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));





share|improve this answer































    0














    I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:




    • Create new 2D array

    • Iterate over each row and put given array into it


    Example:



    public static int createArray(int arr, int totalRows) {
    int res = new int[totalRows][arr.length];

    for (int row = 0; row < totalRows; row++)
    System.arraycopy(arr, 0, res[row], 0, res[row].length);

    return res;
    }





    share|improve this answer























    • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
      – WhatsThePoint
      Nov 12 '18 at 8:29











    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',
    autoActivateHeartbeat: false,
    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%2f53257208%2fauto-generate-variable-size-2d-array-from-1d-arrays%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This can be achieved using stream api in java



    int arrInt = { { 1, 2 }, { 3, 4, 5 } };
    int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
    System.out.println(Arrays.toString(result));





    share|improve this answer




























      0














      This can be achieved using stream api in java



      int arrInt = { { 1, 2 }, { 3, 4, 5 } };
      int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
      System.out.println(Arrays.toString(result));





      share|improve this answer


























        0












        0








        0






        This can be achieved using stream api in java



        int arrInt = { { 1, 2 }, { 3, 4, 5 } };
        int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
        System.out.println(Arrays.toString(result));





        share|improve this answer














        This can be achieved using stream api in java



        int arrInt = { { 1, 2 }, { 3, 4, 5 } };
        int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
        System.out.println(Arrays.toString(result));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 8:38

























        answered Nov 12 '18 at 8:30









        Navin Gelot

        2531216




        2531216

























            0














            I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:




            • Create new 2D array

            • Iterate over each row and put given array into it


            Example:



            public static int createArray(int arr, int totalRows) {
            int res = new int[totalRows][arr.length];

            for (int row = 0; row < totalRows; row++)
            System.arraycopy(arr, 0, res[row], 0, res[row].length);

            return res;
            }





            share|improve this answer























            • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
              – WhatsThePoint
              Nov 12 '18 at 8:29
















            0














            I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:




            • Create new 2D array

            • Iterate over each row and put given array into it


            Example:



            public static int createArray(int arr, int totalRows) {
            int res = new int[totalRows][arr.length];

            for (int row = 0; row < totalRows; row++)
            System.arraycopy(arr, 0, res[row], 0, res[row].length);

            return res;
            }





            share|improve this answer























            • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
              – WhatsThePoint
              Nov 12 '18 at 8:29














            0












            0








            0






            I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:




            • Create new 2D array

            • Iterate over each row and put given array into it


            Example:



            public static int createArray(int arr, int totalRows) {
            int res = new int[totalRows][arr.length];

            for (int row = 0; row < totalRows; row++)
            System.arraycopy(arr, 0, res[row], 0, res[row].length);

            return res;
            }





            share|improve this answer














            I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:




            • Create new 2D array

            • Iterate over each row and put given array into it


            Example:



            public static int createArray(int arr, int totalRows) {
            int res = new int[totalRows][arr.length];

            for (int row = 0; row < totalRows; row++)
            System.arraycopy(arr, 0, res[row], 0, res[row].length);

            return res;
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 '18 at 8:42

























            answered Nov 12 '18 at 7:23









            oleg.cherednik

            5,87821017




            5,87821017












            • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
              – WhatsThePoint
              Nov 12 '18 at 8:29


















            • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
              – WhatsThePoint
              Nov 12 '18 at 8:29
















            Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
            – WhatsThePoint
            Nov 12 '18 at 8:29




            Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
            – WhatsThePoint
            Nov 12 '18 at 8:29


















            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%2f53257208%2fauto-generate-variable-size-2d-array-from-1d-arrays%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







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation