如何从ArraryList中选择并打印出多个项目?爪哇

时间:2019-04-17 13:04:11

标签: java

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

我在互联网上看过,并且已经尝试过。它可以工作,但是当我复制它时,它会打印两次相同的食物(总是)。

rdlc

我真正想要的是从上面的ArrayList中打印出两个项目。没什么。

7 个答案:

答案 0 :(得分:2)

首先,当您显式转换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));
    }
}

答案 1 :(得分:1)

最简单的方法是随机播放列表并获得子列表

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);

答案 2 :(得分:0)

我认为她的问题是演员。 Math.random为您提供了01之间的数字。将其强制转换为int时,只需删除小数点后的所有内容,因此最终得到0。因此它将返回列表中的第一项。

使用(int) (Math.random() * foodList.size())有望为您带来更好的答案。

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

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

答案 3 :(得分:0)

您的问题出在

func setBarChart(barchart: BarChartView, chartData: BarData){

        let groupSpace = 0.16
        let barSpace = 0.03
        let barWidth = 0.25

        let yearlyData = [YearlyData] (chartData.data!)

        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


        let tempData1 = [ReportData] (yearlyData[0].data!)
        let tempData2 = [ReportData] (yearlyData[1].data!)
        let tempData3 = [ReportData] (yearlyData[2].data!)

        var yearlyData1 = [Double]()
        var yearlyData2 = [Double]()
        var yearlyData3 = [Double]()

        for i in 0..<12 {
            yearlyData1.append(Double(tempData1[i].value!)!)
            yearlyData2.append(Double(tempData2[i].value!)!)
            yearlyData3.append(Double(tempData3[i].value!)!)
        }

        barchart.noDataText = "no_data_found".localized
        barchart.noDataTextColor = textColour
        barchart.chartDescription?.text = ""
        barchart.drawValueAboveBarEnabled = true
        barchart.legend.enabled = true

        barchart.xAxis.drawGridLinesEnabled = false
        barchart.xAxis.labelTextColor = textColour
        barchart.xAxis.labelPosition = .bottom
        barchart.xAxis.labelCount = months.count
        barchart.xAxis.labelRotationAngle = 0.0
        barchart.xAxis.valueFormatter = IndexAxisValueFormatter(values:months)
        barchart.xAxis.granularityEnabled = true
        barchart.xAxis.granularity = 1
        barchart.xAxis.axisMinimum = 0
        barchart.xAxis.centerAxisLabelsEnabled = true
        barchart.xAxis.axisMaximum = 12
        barchart.xAxis.labelPosition = .top
        barchart.rightAxis.enabled = false
        barchart.leftAxis.labelTextColor = textColour
        barchart.leftAxis.drawGridLinesEnabled = false
        barchart.leftAxis.axisMinimum = 0

        barchart.fitBars = true

        var dataEntries1: [BarChartDataEntry] = []
        var dataEntries2: [BarChartDataEntry] = []
        var dataEntries3: [BarChartDataEntry] = []
        var counter = 0.0

        for i in 0..<months.count {
            let dataEntry1 = BarChartDataEntry(x: counter, y: yearlyData1[i])
            dataEntries1.append(dataEntry1)
            let dataEntry2 = BarChartDataEntry(x: counter, y: yearlyData2[i])
            dataEntries2.append(dataEntry2)
            let dataEntry3 = BarChartDataEntry(x: counter, y: yearlyData3[i])
            dataEntries3.append(dataEntry3)
            counter += 1.0
        }

        let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: "2017")
        chartDataSet1.colors = [UIColor.red]
        chartDataSet1.valueColors = [textColour]

        let chartDataSet2 = BarChartDataSet(values: dataEntries2, label: "2018")
        chartDataSet2.colors = [UIColor.orange]
        chartDataSet2.valueColors = [textColour]

        let chartDataSet3 = BarChartDataSet(values: dataEntries3, label: "2019")
        chartDataSet3.colors = [UIColor.green]
        chartDataSet3.valueColors = [textColour]

        let chartData = BarChartData()
        chartData.addDataSet(chartDataSet1)
        chartData.addDataSet(chartDataSet2)
        chartData.addDataSet(chartDataSet3)
        chartData.barWidth = barWidth
        barchart.data = chartData

        barchart.setVisibleXRangeMaximum(4.0)

        barchart.groupBars(fromX: 0, groupSpace: groupSpace, barSpace: barSpace)
        barchart.animate(yAxisDuration: 0.5, easingOption: .easeInExpo)
    }

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

Math.random()

答案 4 :(得分:0)

您可以通过这种方式来确保您有2个不同的数字:

创建一个从0foodList.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)));

答案 5 :(得分:0)

您必须在math.random括号中包含FloodList.size,您总是会得到0,因此您总是在ArrayList中显示第一个食物

尝试使用此:

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

答案 6 :(得分:0)

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

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