# Saving the output of array permutations into a 2D array

35
June 13, 2019, at 2:00 PM

I am trying to save the permutation result of an array into a 2d array. let's say I have an array {1,2,3,4}, I created a 2D array with rows: N! and columns 4. Is it possible to save all the results into 2D array. For example I have the following output I want to save it into 2D array

[1, 2, 3, 4] [1, 2, 4, 3]

Your question is unclear. Just in case you are wondering how to create and populate a "2D array" here's what I can tell :

import java.util.Arrays;
public class ArraysOfArrays {
public static void main(String[] args) {
// This is your '1D' array
int[] init = {1, 2, 3, 4};
//This is you '2D' array, 24 possibilities of 4 items in order
int[][] combinations = new int[24][4];
// fill your 2D array with the combinations you compute like that :
combinations[0] = new int[] {1,2,3,4};
combinations[1] = new int[] {1,2,4,3};
combinations[2] = new int[] {1,3,2,4};
combinations[3] = new int[] {1,3,4,2};
combinations[4] = new int[] {1,4,2,3};
combinations[5] = new int[] {1,4,3,2};
//...
combinations[23] = new int[] {4,3,2,1};
// or you can assign the individual values one by one :
combinations[0][0] = 1;
combinations[0][1] = 2;
combinations[0][2] = 3;
combinations[0][3] = 4;
System.out.println(Arrays.toString(combinations[3]));
}
}

Hope that helps!

If you want to store many arrays in a "2D" array, then use a loop as follows:

int size = 24;  //this should be n! for permutations of n values.
combs[][] = new int[size][];
for (int i = 0; i < size; i++) {
combs[i] = getNextPermutationArray();
}

And Java does not have true multidimensional arrays. It simply has arrays of arrays of arrays, etc.

POPULAR ONLINE

### How to convert a string list to my specific java class

My problem is that I write a method that takes as parameter a list of my specific java class, and convert it to a String thanks to a getter of an information of the class and I return the string listI would now like to do the opposite, that is, I give it a string...

28

### Jenkins Sonar Scanner vs Maven sonar:sonar goal

I tried running a sonar analysis using a java project on Jenkins using the standard maven sonar:sonar goal and also using the post step>> Execute SonarQube scannerThe maven goal I used is

33

### Shuffling Characters in a String [on hold]

guysHow do I shuffle characters in a string using only Math

28

### Unable to build with Java on Sublime Text

I am trying to use java on sublime textI set the build system to JavaC, and put some simple code from a text book I have and press ctrl+B to run the program

59