# How to store 2D array in another array

• February 7th, 2014, 10:17 AM
Infanta
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++;
}
• February 7th, 2014, 10:23 AM
Norm
Re: How to store 2D array in another array
Quote:

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];
• February 7th, 2014, 10:58 AM
GregBrannon
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.

• February 7th, 2014, 11:14 PM
Infanta
Re: How to store 2D array in another array
Quote:

Originally Posted by Norm
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 .....
• February 8th, 2014, 12:09 AM
GregBrannon
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.
• February 8th, 2014, 07:46 AM
Wizand
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
Code Java:

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 ?
Code Java:

ArrayList<MyMartix> listOfMatrices = new ArrayList<MyMatrix>();
for (int i = 0; i < 10; i++)

• February 8th, 2014, 08:13 AM
GregBrannon
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.
• February 8th, 2014, 08:19 AM
Norm
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.
• February 8th, 2014, 08:49 AM
Wizand
Re: How to store 2D array in another array
Quote:

Originally Posted by GregBrannon
@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
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 :)