【问题标题】:How to select and print out multiple items from an ArraryList? Java如何从 ArrayList 中选择并打印出多个项目?爪哇
【发布时间】:2019-04-17 13:04:11
【问题描述】:

我刚刚创建了一个包含食物列表的 ArrayList,我想从 ArrayList 中随机打印出两种食物。我该怎么做呢?

我在网上看过,我试过了。它有效,但是当我复制它时,它会打印出两次相同的食物(总是)。

public class Main {


    public static void main(String[] args) {


        int pizza = 1;
        int burger = 2;
        int fries = 3;
        int FishnCHips = 4;



        List<String> foodList = new ArrayList<String>();


        System.out.print("Which food do you want?");
        foodList.add("pizza");
        foodList.add("burger");
        foodList.add("fries");
        foodList.add("Fish and chips'");

        int randomIndex = (int) (Math.random()) * foodList.size();
        System.out.println("Two random foods : " + foodList.get(randomIndex));

        int secondrandomIndex = (int) (Math.random()) * foodList.size();
        System.out.println("Two random foods : " + foodList.get(secondrandomIndex));


我真正想要的只是从上面的 ArrayList 中打印出两个项目。没有别的了。

【问题讨论】:

标签: java


【解决方案1】:

首先,当您显式转换 Math.random() * foodList.size() 时,您将始终以 0 结束。您可以改用 Math.util.Random。

import java.util.*;
public class Main {


    public static void main(String[] args) {


        int pizza = 1;
        int burger = 2;
        int fries = 3;
        int FishnCHips = 4;



        List<String> foodList = new ArrayList<String>();


        System.out.print("Which food do you want?");
        foodList.add("pizza");
        foodList.add("burger");
        foodList.add("fries");
        foodList.add("Fish and chips'");

        Random rand = new Random();

        int randomIndex = rand.nextInt(foodList.size());

        System.out.println("Two random foods : " + foodList.get(randomIndex));

        int secondRandomIndex = rand.nextInt(foodList.size());

        while(secondRandomIndex==randomIndex){
            // To prevent both of the indexes to be the same
            secondRandomIndex = rand.nextInt(foodList.size());
        }

        System.out.println("Two random foods : " + foodList.get(secondRandomIndex));
    }
}

【讨论】:

    【解决方案2】:

    最简单的方法是打乱您的列表并获得一个子列表

    List<String> list = Arrays.asList("pizza","burger","fries","Fish and chips");
    Collections.shuffle(list); 
    int randomItemsLength = 2; 
    List<String> randomItems = list.subList(0, randomItemsLength);
    
    System.out.println(randomItems);
    

    【讨论】:

      【解决方案3】:

      您可以这样做以确保您有 2 个不同的数字:

      创建从0 到foodList.size() 的整数列表

      List<Integer> indexes = IntStream.range(0, foodList.size())
                                       .boxed()
                                       .collect(Collectors.toList());
      

      随机播放

      Collections.shuffle(indexes);
      

      选择前 2 个:

      System.out.println("Two random foods : ");
      System.out.println(foodList.get(indexes.get(0)));
      System.out.println(foodList.get(indexes.get(1)));
      

      【讨论】:

        【解决方案4】:

        我认为她的问题在于选角。 Math.random 为您提供介于 0 和 1 之间的数字。当您将其转换为 int 时,您只需删除小数点后的所有内容,因此您最终会得到 0。所以它将返回列表中的第一项。

        使用(int) (Math.random() * foodList.size()) 希望能给你一个更好的答案。

        您也可以考虑只使用Random().nextInt(foodList.size())。

        This 我认为很好地介绍了在 Java 中获取随机数的不同方法。

        【讨论】:

          【解决方案5】:

          你的问题出在:

          (Math.random()) * foodList.size();
          

          foodList.size(); 周围没有括号。这意味着您没有修改 Math.random() 范围。该行应如下所示:

          (Math.random() * foodList.size());
          

          【讨论】:

            【解决方案6】:

            使用正确的演员表会有所帮助。

            int randomIndex = Double.valueOf((Math.random()) * foodList.size()).intValue();
            

            【讨论】:

              【解决方案7】:

              你必须在 math.random 括号内包含 floodList.size,你总是得到 0,所以你总是显示 ArrayList 中的第一个食物

              试试这个:

              Math.floor(Math.random() * foodList.size());
              

              【讨论】:

                猜你喜欢
                • 2021-03-11
                • 1970-01-01
                • 2012-08-15
                • 2020-07-21
                • 2015-06-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-09-05
                相关资源
                最近更新 更多