Getting specific elements from arraylist

家住魔仙堡 提交于 2020-06-01 06:25:13

问题


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 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:


Here are some examples for solving your problem.

For-each loop

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

indexed for loop

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

Streams (JDK 8 >)

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

I hope it's helpful.




回答2:


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

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
}

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




回答3:


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("*****")
        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));
    }



回答4:


Do it as follows:

allCustomers = tbl.getCustomers(); 
for (Customer customer: allCustomers) {
    // Assuming that the class, Customer has getCustomerOrder() method which returns the customer's order
    // Also assuming that the class, Order has toString method overridden to display the food items in it
    System.out.println(customer.getCustomerOrder()); 
}

Note: I see that you have put this iterating code inside initialize() where it doesn't make sense as tbl.getCustomers() will return you an empty list which means the control will never enter the loop. This iteration should happen at some event.




回答5:


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 (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
}


来源:https://stackoverflow.com/questions/61105027/getting-specific-elements-from-arraylist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!