简体   繁体   中英

How can I choose a random element from an ArrayList if it changes size?

In my code " count0 ", " count1 " and " count2 " receive random values and "totalCount" is a sum of them. Here I have simplified it.

With this code I can choose a random element of the ArrayList but if for example "count0" is equal to 0 it will remove the index 0 from the ArrayList not only the first time but every iteration, causing error if this happens. The idea is how to randomly choose ingredients from a pantry and when one is exhausted you can still choose from the rest.

Thanks for the help =)

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

public class RndNumArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int totalCount=4, count0=1, count1=1, count2=2;
        Random rnd = new Random();
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(0, 1, 2));

        while(totalCount > 0){

            int rand = rnd.nextInt(numbers.size());
            int num = numbers.get(rand);
            if (num == 0) {
                count0--;
                if (ccount0 == 0) {
                    numbers.remove(0);
                }
            }
            if (num == 1) {
                count1--;
                if (count1 == 0) {
                    numbers.remove(1);
                }
            }
            if (num == 2) {
                count2--;
                if (count2 == 0) {
                    numbers.remove(2);
                }
            }
            totalCount--;
            System.out.println(num);
        }
    }    
}

Use two List counts (how many count of product you have) and numbers (product number):

public static void main(final String[] args)
{
    final int totalCount = 4;
    final Random rnd = new Random(); 
    final List<Integer> numbers = new ArrayList<>(Arrays.asList(0, 1, 2));
    final List<Integer> counts = new ArrayList<>(Arrays.asList(1, 1, 2));

    for (int i = 1; i < totalCount; i++)
    {
        final int rand = rnd.nextInt(numbers.size());
        final int num = numbers.get(rand);
        int count = counts.get(rand);
        count--;
        if (count < 1)
        {
            numbers.remove(rand);
            counts.remove(rand);
        }else
        {
            counts.set(rand, count);
        }
        System.out.println(num);
    }
}

Or you can delete numbers List if you product numbers is always 0,1,2,3...

Thanks, guys. You guys are great. I'll try all your suggestions but I've come up with a solution that works. Instead of deleting the items from the list I have given them a value that I will control with a do-while so that you only choose the available ones.

My code is a modification of the smoking problem (threads and synchronization). In my code each smoker has a certain number of cigars and can finish before the rest, so the agent does not have to put ingredients for him.

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

public class RndNumArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    int totalCount=4, count0=1, count1=1, count2=2;
    Random rnd = new Random();
    ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(0, 1, 2));

    while(totalCount>0){
        int rand;
        int num;

        do{
        rand = rnd.nextInt(numbers.size());
        num = numbers.get(rand);
        } while(num==3);

        if (num == 0) {
            count0--;
            if (ccount0 == 0) {
                numbers.set(0,3);
            }
        }
        if (num == 1) {
            count1--;
            if (count1 == 0) {
                numbers.set(1,3);
            }
        }
        if (num == 2) {
            count2--;
            if (count2 == 0) {
                numbers.set(2,3);
            }
        }
        totalCount--;
        System.out.println(num);
    }
    }    
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM