I have generated a list of 20 unique random numbers between 1 and 300, and used that for what I need to use it for. However, I also need the numbers that have not made it to the list to be added into another list for use in another function.
Here is the code I have used to generate the random list of 20 numbers:
JToggleButton[][] p = new JToggleButton[5][4];
ArrayList<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
int randomNum = rand.nextInt((300 - 1) + 1) + 1;
while (list.contains(randomNum)) {
randomNum = rand.nextInt((300 - 1) + 1) + 1;
}
list.add(randomNum);
// rest of code that I need the random number list for
I need to get the other 280 numbers not included in this 20 item list into another list, but I am unsure how to actually get these 'unused' numbers
If you truly need two lists exactly as you say, then here's a method of doing that:
final int numberOfInts = 300;
List<Integer> excluded = new ArrayList<>();
for (int i = 1; i <= numberOfInts ; i++) {
excluded.add(i);
}
Random rand = new Random();
List<Integer> included = new ArrayList();
for (int i = 0; i < p.length; i++) {
included.add(excluded.remove(rand.nextInt(excluded.size())));
}
Took me forever to understand what your question was asking.
ArrayList<Integer> otherNumbers = new ArrayList<Integer>();
for(int x = 0; x < list.size(); x++) {
if(!list.contains(x+1)) {
otherNumbers.add(x+1);
}
}
You can just populate a new list by skipping over the values the 20 number list contains.
I think the best way to do this would be to generate a separate list and populate it with every number between 1 and 300. After that, just iterate through your list of generated random numbers and remove those elements from your 1 through 300 list. Something like this..
ArrayList<Integer> list300 = new ArrayList<Integer>();
For (int i = 1; i <= 300; i++) {
list300.add(i);
}
For (int i = 0; i < list.size(); i++) {
if (list300.contains(list[i])) {
list300.remove(Integer.vaueOf(list[i]));
}
}
You can create a List
with all 300 numbers to begin with, and then when you add a random number to the other list, just remove the same number from the List
with all of the values:
Create the list:
ArrayList<Integer> excludedNumbers = new ArrayList<>(300);
for (int i = 1; i <= 300; i++){
excludedNumbers.add(i);
}
Add a single line to your current code:
ArrayList<Integer> randomList = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < p.length; i++) {
for (int i = 0; i < p[i].length; i++) {
int randomNum = rand.nextInt((300 - 1) + 1) + 1;
while (randomList.contains(randomNum)) {
randomNum = rand.nextInt((300 - 1) + 1) + 1;
}
excludedNumbers.remove((Integer) randomNum); // the new line
randomList.add(randomNum);
}
}
The cast of Integer
ensures the List
uses the Object
remove function rather than the int
remove option.
I hope I got your question right as it was kind of hard to understand for me. I would do it like this:
ArrayList<Integer> notIncludedNumbers = new ArrayList<Integer>();
for (int i = 0; i <= 300; i++){
if (!list.contains(i))
notIncludedNumbers.add(i);
}
You iterate over all the numbers from 1 to 300 and add them to the notIncludedNumbers
list if they are not contained in your list with the 20 random numbers. I hope I could help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.