java – generate all 3*3 two dimentional arrays – Education Career Blog

how would i write a function in java that generates all the 3*3 multidimensional arrays such that every number between 1…9 only appears once?

thanks!

,

Basically, you are looking for all permutations of the series 1, 2, ... 9, but notice that you split them up to a matrix of 3x3.

It is easy to prove mathematically that the number of permutations is 9! (factorial 9).

There are many algorithms to generate permutations. Choose the one you find the most convenient. For example, at wikipedia.

,

I’d start small, how would you do it for a 2*2 arrays? Do it manually – i.e. start to write down all the possible 2*2 arrays. How do you do it? Then think how you can do that in code. If you do it for a 2*2 you should be able to see if your results look good by eye so you’ll know if you are on the right track.

This is basically a permutation problem, once you figure it out for small numbers the same solution should scale.

,

Note: this is not actually homework according to the OP.

How far have you gotten? It sounds like homework, and that is fine, SO will probably help, but you should show us how much you’ve done. SO is about making better programmers, so show us what you can do and we’ll help out

Leave a Comment