I've been tryin to figure out how to pick numbers from the 'choices array' and put then into the results array. Can any body help me. I was thinking of adding a for loop but not sure. The code i got so far is below.

import java.util.Random;
import java.util.ArrayList;

public class PermutationGenerator
   public PermutationGenerator(int length)
      generator = new Random();
      this.length = length;

   public int[] nextPermutation()
      int[] result = new int[length];
      ArrayList<Integer> choices = new ArrayList<Integer>();

      for (int i = 0; i < length; i++)
        choices.add(i + 1);

      return result;

   private Random generator;
   private int length;

Edited by mike_2000_17: Fixed formatting

9 Years
Discussion Span
Last Post by notuserfriendly

and what are the errors you are getting?
what is it that you don't understand? getting a random element, or storing it in the new array?

I just briefly glanced at your code, and I'm not entirely sure, but trying to store an Integer in an int array .. does seem like a compilation error in the making to me :)


I am unclear of what you are trying to do here, so correct me if I haven't understood but instead of copying choices to result and then returning back result, why not return choices.


For one, you don't have a main method (that you showed), so you never constructed a PermutationGenerator object and int[] result = new int[length]; has no effect since it is never called.

Secondly, if you wanted to put the stuff from the result array into the choices ArrayList, it would look like (pseudocode)

for each element in result
add the element from result to the choices arraylist


well do you just want to transfer all the numbers? or just implement a search method that finds your number and them if it is found it places it into the other array?
please be more clear.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.