I am creating a time table generator as secondary school project.I used the following code to generate random collections but I get at least one component being similar. I do not want such as Time table for two class should not match how do I do it.
package xyz;
import java.util.ArrayList;
public class Cls {
public static void main (String args[]){
ArrayList <Integer> previous=getlist();
ArrayList <Integer> present=new ArrayList<>();
while(true){
ArrayList <Integer> list=getlist();
if(previous!=present) {present=list;break;}
else {continue;}
}
System.out.println(""+previous);
System.out.println(""+present);
}
protected static ArrayList<Integer> getlist() {
ArrayList <Integer> day=new ArrayList <>();
while(true){
int a=(int) (1+Math.random()*6);
day.add(a);
if(day.size()==8) break;
}
return day;
}
}
How Do I get previous and present as two different values with no number matching the same? OutPut for above code: [2, 6, 5, 4, 2, 5, 3, 2] [5, 5, 6, 2, 4, 5, 3, 1] BUILD SUCCESSFUL (total time: 4 seconds) //Here at 6th position and 7th position the terms matches. I must have gone wrong somewhere Please help me
Aucun commentaire:
Enregistrer un commentaire