Retrieving a random item from ArrayList [duplicate

2019-01-03 09:30发布

This question already has an answer here:

I'm learning Java and I'm having a problem with ArrayList and RandomGenerator.

I have an object called catalogue which has an array list of objects created from another class called item.

I need a method in catalogue which returns all the information on one of the itemobjects in the list.
The item needs to be selected at random.

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

public class Catalogue
{
    private Random randomGenerator;
    private ArrayList<Item> catalogue;

    public Catalogue ()
    {
        catalogue = new ArrayList<Item>();  
    }

    public Item anyItem()
    {
        int index = randomGenerator.nextInt(catalogue.size());
        return catalogue.get(index);
        System.out.println("Managers choice this week" + anyItem + "our recommendation to you");
    }

When I try to compile I get an error pointing at the System.out.println line saying..

'cannot find symbol variable anyItem'

12条回答
\"骚年 ilove
2楼-- · 2019-01-03 09:58

See https://gist.github.com/nathanosoares/6234e9b06608595e018ca56c7b3d5a57

public static void main(String[] args) {
    RandomList<String> set = new RandomList<>();

    set.add("a", 10);
    set.add("b", 10);
    set.add("c", 30);
    set.add("d", 300);

    set.forEach((t) -> {
        System.out.println(t.getChance());
    });

    HashMap<String, Integer> count = new HashMap<>();
    IntStream.range(0, 100).forEach((value) -> {
        String str = set.raffle();
        count.put(str, count.getOrDefault(str, 0) + 1);
    });

    count.entrySet().stream().forEach(entry -> {
        System.out.println(String.format("%s: %s", entry.getKey(), entry.getValue()));
    });
}

Output:

2.857142857142857

2.857142857142857

8.571428571428571

85.71428571428571

a: 2

b: 1

c: 9

d: 88

查看更多
狗以群分
3楼-- · 2019-01-03 09:59

You must remove the system.out.println message from below the return, like this:

public Item anyItem()
{
    randomGenerator = new Random();
    int index = randomGenerator.nextInt(catalogue.size());
    Item it = catalogue.get(index);
    System.out.println("Managers choice this week" + it + "our recommendation to you");
    return it;
}

the return statement basically says the function will now end. anything included beyond the return statement that is also in scope of it will result in the behavior you experienced

查看更多
The star\"
4楼-- · 2019-01-03 09:59

anyItem has never been declared as a variable, so it makes sense that it causes an error. But more importantly, you have code after a return statement and this will cause an unreachable code error.

查看更多
走好不送
5楼-- · 2019-01-03 10:04
public static Item getRandomChestItem(List<Item> items) {
    return items.get(new Random().nextInt(items.size()));
}
查看更多
Juvenile、少年°
6楼-- · 2019-01-03 10:05

Here you go, using Generics:

private <T> T getRandomItem(List<T> list)
{
    Random random = new Random();
    int listSize = list.size();
    int randomIndex = random.nextInt(listSize);
    return list.get(randomIndex);
}
查看更多
来,给爷笑一个
7楼-- · 2019-01-03 10:05

As I can see the code
System.out.println("Managers choice this week" + anyItem + "our recommendation to you");
is unreachable.

查看更多
登录 后发表回答