从arraylist获取特定元素 [英] Getting specific elements from arraylist

查看:58
本文介绍了从arraylist获取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户订购该订单添加到客户"类时,我有一个使用java fx的餐厅系统.然后将客户添加到表"类中.

I have a resturant system using java fx, when a customer orders that order is added to the 'customer' class. The customer is then added to the 'table' class.

最后,我试图通过遍历每个客户的订单列表来显示每个客户的订单,但我无法弄清楚语法

At the end im trying to display each individual customers order by looping through an array list of them and i cant figure out the syntax

这样设置一个名为allcustomers的数组列表

an arraylist called allcustomers is set like this

allcustomers = tbl.getCustomers();

打印出来看起来像这样

[Customer{customernumber=null, customerorder=[burger'7.99]}]

我试图遍历"allcustomers"数组列表并仅获取食物,但不确定如何做?

这是完整的代码

public class PaymentScreenController {


    public ArrayList<Customer> allcustomers;

    public Customer cus1;
    public ArrayList cusarray;


    private Table tbl = new Table();

    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers();




        // Unsure about how to do this for loop
        for (  : allcustomers) {




        }

任何帮助将不胜感激

推荐答案

像这样使用增强的循环:

Enhanced for loops are used like this:

for (Customer customer : allCustomers) {
   //To display the customer's order or some other attribute
   System.out.println(customer.customerOrder);
}

带有索引的for循环:

With an indexed for loop:

for (int i = 0; i < allCustomers.size(); i ++) {
  Customer customer = allCustomers.get(i);
  System.out.println(customer.customerOrder);
  //This could be turned into one line, but shows how you index in ArrayLists
}

请参阅下面的LinuxServer评论,以获取一个更凉爽的版本,该版本使用流来浏览列表.

See the comment by LinuxServer below for a cooler version that uses streams to go through the list.

这篇关于从arraylist获取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆