0

I have an arrayList LAL that is filled with other arraylists via add function, like that:

for(int i=0;i<v_amountComponentOptions;i++){
    ArrayList AL=new ArrayList<CustomerOrder>();
    LAL.add(AL);
}

Then I add orders (orders have the attribute product and quantity) to this List of Array List, like that:

LAL.get(0).add(order);

When I try to plot the quantity as follows, I get an error. It says that there is no quantity value.

traceln(LAL.get(0).get(0).quantity);

However, when I write;

traceln(LAL.get(0).get(0).quantity);

Then, the output is :

root( product = 1, quantity = 63 )

My point is that I need to get the quantity value. Could anyone tell me how to do that? Thank you very much.

2
  • The two statement behaving differently look the same. Also what does your class look like? Do you have getters and setters? Are the fields accessible? Commented May 16, 2020 at 20:58
  • Post your CustomerOrder class. It looks like quantity either doesn't exist on CustomerOrder or is private. Commented May 16, 2020 at 21:03

1 Answer 1

1

One possiblity is to use a stream and put all the quantity values in a List. Assuming the quantities are Integers and the quantity value is accessible you could do something like this.

List<Integer> list = LAL.stream()
                 .flatMap(List::stream)
                 .map(a->a.quantity)
                 .collect(Collectors.toList());

And if you had getters, this would be even better.

List<Integer> list = LAL.stream()
                 .flatMap(List::stream)
                 .map(CustomerOrder::getQuantity)
                 .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.