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

Thread: How to store 2D array in another array

1. 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. 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];

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

Infanta (February 8th, 2014)

4. 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.

5. Re: How to store 2D array in another array

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 .....

6. 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. 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++)

8. 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. 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.

10. Re: How to store 2D array in another array

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 ---

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