LAST issue! Random permutation array!
I have the array printing out just 0's. I am not sure what i have to input to make it output random digits. I want random digits 0-9, I am not sure what to do. This is the last program i need help on. Thank you for any help!
Code :
/**
This class prints 5 permutations of the numbers 1 through 10.
*/
public class PermutationPrinter
{
public static void main(String[] args)
{
PermutationGenerator gen = new PermutationGenerator(10);
for (int i = 1; i <= 5; i++)
{
for (int n : gen.nextPermutation())
System.out.print(" " + n);
}
}
}
Code :
import java.util.Random;
import java.util.*;
/**
This class generates permutations of a sequence of integers
1...length.
*/
public class PermutationGenerator
{
ArrayList<Integer> number = new ArrayList<Integer>();
int size;
/**
Construct a PermutationGenerator object.
@param length the length of the permutations generated
by this generator.
*/
public PermutationGenerator(int length)
{ size = length;
for (int i = 0; i <= length; i++){
number.add(i);
}
}
/**
Gets the next permutation.
@return the array containing the next permutation
*/
public int[] nextPermutation()
{
int[] x = new int[size];
for( int i = 0; i<size; i++){
Random k = new Random();
int y = k.nextInt(number.size());
}
return x;
}
}
Re: LAST issue! Random permutation array!
In your nextPermutation() method, you are never adding the values from the for loop to your int array. So you are just creating a new array and passing on that empty array, which would contain all 0s
Re: LAST issue! Random permutation array!
That makes sense actually, but how would i go about doing that?
Re: LAST issue! Random permutation array!
Quote:
Originally Posted by
bankoscarpa
That makes sense actually, but how would i go about doing that?
You can just make an assignment statement after the creation of the random number (y). You should assign y to an index of your array.
Re: LAST issue! Random permutation array!