简体   繁体   English

从 arraylist 获取特定元素

[英]Getting specific elements from arraylist

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.我有一个使用 java fx 的餐厅系统,当客户下订单时,该订单被添加到“客户”class。然后客户被添加到“表”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最后,我试图通过遍历他们的数组列表来显示每个单独的客户订单,但我无法弄清楚语法

an arraylist called allcustomers is set like this一个名为 allcustomers 的 arraylist 是这样设置的

allcustomers = tbl.getCustomers();

printed out it looks like this打印出来看起来像这样

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

im trying to loop through the 'allcustomers' arraylist and just get the food items but im unsure how how?我试图遍历“allcustomers”arraylist 并只获取食品,但我不确定如何?

this is the full code这是完整的代码

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




        }

any help would be appreciated thanks任何帮助将不胜感激谢谢

Enhanced for loops are used like this:增强的 for 循环是这样使用的:

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

With an indexed for loop:使用索引 for 循环:

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
}

See the comment by LinuxServer below for a cooler version that uses streams to go through the list.请参阅下面 LinuxServer 的评论,了解通过列表使用到 go 的流的更酷版本。

Here are some examples for solving your problem.以下是解决您的问题的一些示例。

For-each loop For-each 循环

for (Customer customer : allCustomers) {
      //...
 }

indexed for loop索引循环

for (int i = 0; i < allCustomers.size(); i++) {
  Customer customer = allCustomers.get(i);
  //...
}

Streams (JDK 8 >)流(JDK 8 >)

allCustomers.stream().filter(customer -> /*some condition here*/).forEach(customer -> {
  //...
});

I hope it's helpful.希望对您有所帮助。

As I see, Customer has ArrayList of orders nested inside of a Customer object. So, first you need to iterate over all customers in allCustomers and then iterate over all orders in customerOrder list.如我所见,客户有 ArrayList 个订单嵌套在客户 object 中。因此,首先您需要遍历 allCustomers 中的所有客户,然后遍历 customerOrder 列表中的所有订单。 Star output is used to separate orders from each customer.星号output用于区分每个客户的订单。

for (Customer customer : allCustomers) {
        System.out.println("*****")
        for(String singleOrder: customer.customerOrder){
            System.out.println(singleOrder);
      }
    }

Also, you can do it like this:另外,您可以这样做:

for (Customer customer : allCustomers) {
        System.out.println("*****")
        System.out.println(Arrays.toString(customer.customerOrder));
    }

I have a resturant system using java fx, when a customer orders that order is added to the 'customer' class.我有一个使用 java fx 的餐厅系统,当客户订购该订单时,该订单被添加到“客户”class。 The customer is then added to the 'table' class.然后将客户添加到“表”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最后,我试图通过遍历他们的数组列表来显示每个客户的订单,但我无法弄清楚语法

an arraylist called allcustomers is set like this一个名为 allcustomers 的 arraylist 设置如下

allcustomers = tbl.getCustomers();

printed out it looks like this打印出来看起来像这样

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

im trying to loop through the 'allcustomers' arraylist and just get the food items but im unsure how how?我试图遍历“allcustomers”arraylist,然后只拿到食物,但我不确定怎么做?

this is the full code这是完整的代码

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




        }

any help would be appreciated thanks任何帮助将不胜感激谢谢

for (Customer customer : allcustomers) { // Iterate through all customers
  print("Customer Number: " + customer.customernumber); // Print Customer Number
  print("Customer Order: " + customer.customerorder.toString()); // Print the order array
}

Using a normal for loop使用普通的 for 循环

for (int i = 0; i < allcustomers.length; i++) {
  print("Customer Number: " + allcustomers[i].customernumber); // Print Customer Number
  print("Customer Order: " + allcustomers[i].customerorder.toString()); // Print the order array
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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