I got my code to work in the expected way, but when i try to make more than one list of permutations it gives me the first one and doesn't change it around. I need to make a list of permutations from 1 to 10 using an Arraylist and to catch one error that can occur in the code. Then we have to duplicate that code so that we get 9 DIFFERENT lines of permutations.
import java.util.ArrayList;
import java.util.Random;
public class Permutations
{
ArrayList <Integer> Perm = new ArrayList <Integer> ();
ArrayList <Integer> Print = new ArrayList <Integer> ();
int counter = 9;
int List = 1;
public void ArrayList()
{
Perm.add(1);
Perm.add(2);
Perm.add(3);
Perm.add(4);
Perm.add(5);
Perm.add(6);
Perm.add(7);
Perm.add(8);
Perm.add(9);
Perm.add(10);
}
public void PermutationGenerator()
{
ArrayList();
Random rand = new Random();
int value = rand.nextInt(10) + 1;
while(Print.size() < 10)
{
try
{
while(Print.contains(value))
{
value = rand.nextInt(10) + 1;
}
Print.add(value);
Perm.remove(value);
}
catch(IndexOutOfBoundsException a)
{
System.out.println("");
}
}
System.out.println("List" + List + ":" + Print.toString());
List++;
if(List < 10)
{
PermutationGenerator();
}
}
This is what is printed out:
List1:[9,6,5,4,10,2,8,7,1,3]
List2:[9,6,5,4,10,2,8,7,1,3]
List3:[9,6,5,4,10,2,8,7,1,3]
List4:[9,6,5,4,10,2,8,7,1,3]
List5:[9,6,5,4,10,2,8,7,1,3]
List6:[9,6,5,4,10,2,8,7,1,3]
List7:[9,6,5,4,10,2,8,7,1,3]
List8:[9,6,5,4,10,2,8,7,1,3]
List9:[9,6,5,4,10,2,8,7,1,3]
Aucun commentaire:
Enregistrer un commentaire