Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to store 2D array in another array

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to store 2D array in another array

    I want to generate 30 random 2D array (35 * 35 matrix) and store in single array... Is it possible ????
    I have generated 30 random 2D array using the following code . But how to store that 2D (35 * 35 matrix)

    while(k <= 30){
    for (int i = 0; i < 35; i++){
    for (int j = 0; j < 35; j++) {
    Random rand = new Random();
    pop[i][j] = rand.nextDouble();
    }
    }
    k++;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to store 2D array in another array

    2D array (35 * 35 matrix) and store in single array
    That sounds like a 3D array. The first dim contains the 2D array.
    double[][][] threeD = new double[numberOf2D][35][35];
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Infanta (February 8th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to store 2D array in another array

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    I believe you've been asked to post your code correctly more than once already. If you'd like help with code tags, please ask.

  5. #4
    Junior Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to store 2D array in another array

    Quote Originally Posted by Norm View Post
    That sounds like a 3D array. The first dim contains the 2D array.
    double[][][] threeD = new double[numberOf2D][35][35];

    Thank you :-) ... If i get some sample code to use this it will be helpful for me. How to initialize and use this 3D array .....

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to store 2D array in another array

    The same way you initialize a 1D, 2D, and xD array. Please show how you intend to use the array, attempt to initialize it, and ask specific questions about your code. Don't forget to post your code in code tags.

  7. #6
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: How to store 2D array in another array

    Hi,

    The suggested method certainly works, but wouldn't it be more Java-like to steer people to more object oriented way rather than just brute through raw arrays? What are we now, C barbarians?

    Maybe encapsulate the matrix to a class
    class MyMatrix {
     
    private Double[][] actuaArray;
     
    public MyMatrix(int size) {
    actualArray = new Double[size][size];
    populateMe();
    }
     
    private void populateMe() {
    //that array fill logic here
    }
     
    //+accessors, +mutators
    }

    And then just keep those matrices in a neat array ?
    ArrayList<MyMartix> listOfMatrices = new ArrayList<MyMatrix>();
    for (int i = 0; i < 10; i++)
    listOfMatrices.add(new MyMatrix(35));

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to store 2D array in another array

    @Wizand: Please direct your responses to the OP's question rather than to those who have already responded. Everyone should be able to post their ideas, suggestions, and best efforts here without concern of criticism or derision. Correction, when needed, is certainly acceptable, but it can be done without characterizing a specific member's contribution or that member in a negative way. And in case your comments were meant to be humorous ("I included a smiling emoticon!"), remember than senses of humor vary widely across our international members and humor is not communicated easily in this medium.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to store 2D array in another array

    I think Early students need to build a familiarity with basic programming before moving into OOP. Trying to force them into using classes too soon can lead to frustration. Later in their learning they can move to designing with classes.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: How to store 2D array in another array

    Quote Originally Posted by GregBrannon View Post
    @Wizand: Please direct your responses to the OP's question rather than to those who have already responded. Everyone should be able to post their ideas, suggestions, and best efforts here without concern of criticism or derision. Correction, when needed, is certainly acceptable, but it can be done without characterizing a specific member's contribution or that member in a negative way. And in case your comments were meant to be humorous ("I included a smiling emoticon!"), remember than senses of humor vary widely across our international members and humor is not communicated easily in this medium.
    My intentions were certainly not meant to criticize or judge the other replies. As i said, the raw array style is solid and valid in every way. I was trying to bring discussion about the best solution in good spirits and with constructive manner. My apologies for not knowing the proper ways of ways these forums quite yet. I will try to be more considerate in the future.

    --- Update ---

    Quote Originally Posted by Norm View Post
    I think Early students need to build a familiarity with basic programming before moving into OOP. Trying to force them into using classes too soon can lead to frustration. Later in their learning they can move to designing with classes.
    That is certainly the way i was brought up, but my opinion on the matter differs a bit. I think ( and as i stated, this is only my -probably quite uneducated- opinion on the matter) it would be worth considering to teach people to think and conceptualize OO-way first, and later learn the actual groundwork of iterating through arrays, pointer math, recursive silliness and all that. The IDEs and tools have developed to the point where kids can make software with drag'n'dropping blocks of logic to a diagram and don't have to worry about array sizes and casting errors directly. So it maybe makes sense to get people to think the "right way" first before using time to fiddle with the details. But maybe this is a discussion for another topic

    All in all, your answer definitely answered to the OP's question more directly than mine.

Similar Threads

  1. How to store char variable in an array
    By ashboi in forum Java Theory & Questions
    Replies: 1
    Last Post: November 9th, 2012, 12:48 PM
  2. How can I store a double array in mysql
    By luck999 in forum JDBC & Databases
    Replies: 1
    Last Post: March 15th, 2012, 05:42 AM
  3. array to store multiple names and data
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 6th, 2011, 05:03 AM
  4. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM
  5. Store Values in 2D array and output to textarea
    By raverz1tildawn in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2011, 03:13 PM