从数组列表中获取特定元素

huangapple go评论66阅读模式
英文:

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.

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 = 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?**

this is the full code
public class PaymentScreenController {

    public ArrayList<Customer> allcustomers;

    public Customer cus1;
    public ArrayList<Customer> cusarray;

    private Table tbl = new Table();

    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers();

        // Unsure about how to do this for loop
        for (Customer customer : allcustomers) {
            // Your code to extract food items from each customer's order
            // For example:
            List<String> foodItems = customer.getFoodItems(); // Replace with actual method name
            for (String foodItem : foodItems) {
                // Process each food item, like printing or storing
            }
        }
    }
}

(Note: I've assumed that you have a method getFoodItems() in your Customer class that returns a list of food items from the customer's order. Replace "getFoodItems()" with the actual method name if it's different in your code.)

英文:

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

an arraylist called allcustomers is set like this

allcustomers = tbl.getCustomers();

printed out it looks like this

[Customer{customernumber=null, customerorder=[burger&#39;7.99]}]

im trying to loop through the 'allcustomers' arraylist and just get the food items but im unsure how how?

this is the full code

public class PaymentScreenController {

    
    public ArrayList&lt;Customer&gt; 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

答案1

得分: 1

增强型for循环的使用方式如下所示:

for (Customer customer : allCustomers) {
   // 显示顾客的订单或其他属性
   System.out.println(customer.customerOrder);
}

使用索引for循环:

for (int i = 0; i < allCustomers.size(); i++) {
  Customer customer = allCustomers.get(i);
  System.out.println(customer.customerOrder);
  // 这可以被转化为一行,但展示了如何在ArrayList中使用索引
}

请参阅下面LinuxServer的评论,其中有一个更酷的版本,使用流(streams)遍历列表。

英文:

Enhanced for loops are used like this:

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

With an indexed for loop:

for (int i = 0; i &lt; 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.

答案2

得分: 1

以下是解决您问题的一些示例。

For-each循环

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

索引for循环

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

流(Streams)(JDK 8及以上)

allCustomers.stream().filter(customer -> /*在此添加条件*/).forEach(customer -> {
    //...
});

希望对您有所帮助。

英文:

Here are some examples for solving your problem.

For-each loop

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

indexed for loop

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

Streams (JDK 8 >)

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

I hope it's helpful.

答案3

得分: 0

根据我的理解,客户拥有嵌套在客户对象内部的订单ArrayList。
因此,首先您需要遍历allCustomers中的所有客户,然后遍历customerOrder列表中的所有订单。
星号输出用于区分每个客户的订单。

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

另外,您也可以像这样实现:

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

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.
Star output is used to separate orders from each customer.

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

Also, you can do it like this :

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

答案4

得分: -1

for (Customer customer : allcustomers) { // 遍历所有顾客
  print("Customer Number: " + customer.customernumber); // 打印顾客编号
  print("Customer Order: " + customer.customerorder.toString()); // 打印订单数组
}

使用普通的 for 循环

for (int i = 0; i < allcustomers.length; i++) {
  print("Customer Number: " + allcustomers[i].customernumber); // 打印顾客编号
  print("Customer Order: " + allcustomers[i].customerorder.toString()); // 打印订单数组
}
英文:
for (Customer customer : allcustomers) { // Iterate through all customers
  print(&quot;Customer Number: &quot; + customer.customernumber); // Print Customer Number
  print(&quot;Customer Order: &quot; + customer.customerorder.toString()); // Print the order array
}

Using a normal for loop

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

huangapple
  • 本文由 发表于 2020年4月9日 00:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61105027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定