On 12 mrt, 23:26, "John B. Matthews" <nos...@nospam.invalid> wrote:
In article
<3611832d-daba-4193-b085-cdfc92354...@g4g2000yqa.googlegroups.com>,
dendeezen <tsd35...@scarlet.be> wrote:
Thanks for the great help. I solved the problems. But I have still
one left.
How can I copy the arry lotto into the arry list, so that the are
really identical, so that
I infer that you want to compare both the original and sorted arrays.
Here's how I'd organize the code to keep the implementation private. I
sense this is homework, but I don't know the objective. I've shown ways
to copy, sort and format an array; I've left selecting without
replacement as an exercise.
import java.util.Arrays;
import java.util.Random;
public class Lotto {
private static final Random generator = new Random();
private static final int MAX = 6;
private int[] picks = new int[MAX];
public static void main(String[] args) {
Lotto lotto = Lotto.newInstance();
System.out.println(lotto.original());
System.out.println(lotto.sorted());
}
public static Lotto newInstance() {
return new Lotto();
}
private Lotto() {
for (int i = 0; i < picks.length; i++) {
picks[i] = pickOne();
}
}
public String original() {
return toString(picks);
}
public String sorted() {
int[] temp = Arrays.copyOf(picks, picks.length);
Arrays.sort(temp);
return toString(temp);
}
private String toString(int[] a) {
StringBuilder sb = new StringBuilder();
for (int i : a) {
sb.append(i);
sb.append(' ');
}
return sb.toString();
}
private int pickOne() {
// TODO avoid duplicates
return generator.nextInt(42) + 1;
}
}
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Sorry, things are not clear.