| | |
Array
![]() |
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;
}
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;
}
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
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Also I would like to suggest to please put your code inside [code=java] and [/code] tags
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
Join Date: Sep 2008
Posts: 2,226
Reputation:
Solved Threads: 273
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
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
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Char Array ib Java?
- Next Thread: Multiline JLabel
Views: 397 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener add android applet application arguments array arrays build c# c++ chat class classes client code combobox compile component constructor convert data database desktop eclipse error event exception file filei/o forloop game givemetehcodez graphics gui helpwithhomework html ide image images inheritance input interface j2me java jframe jpanel jtable jtextarea jtextfield julia lazy linked linked-list list loop looping method methods mobile netbeans newbie node number object output page pattern problem program programming project read recursion regex remove return robot scanner screen search server service set size sms socket software source sql string swing text text-file threads time timer tree url






