• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList loop  RSS feed

 
Isaac Ferguson
Ranch Hand
Posts: 1063
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

having this:


How could I get the price dinamically?




I need to get the price for every article in the array list an compare them

Any idea, please?

Regards, Isaac
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what kind of object is cheese? It looks to me like you have one cheese object, and you set the price to 1. Then you change the price of it to 2, then to 3.

You then add a single cheese to your articles object (and we don't know what type that is).

Having said that, you probably want to look up how to write a "for each" loop in java. The basic syntax is something like this:


so yours would be something like


Please note: it's early, I've had no coffee, so this syntax may not be 100%.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use floating‑point arithmetic for money. Use BigDecimal or integer arithmetic.
Never use floats for anything (unless some part of the API requires a float, e.g. this).

You can get a Stream from a List very easily. But it only works in Java8.
myList.stream().forEach(c -> System.out.println(c));

There is a different and more elegant way to do that.
If in Java7, don't use a for loop. Use a for‑each loop. Look in the Java™ Tutorials, where it is called enhanced for and you use exactly the same syntax for a List as you would for the array they show.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you want a float, don't write:
(float) 1

Write:
1.0f

But why not double type?

Campbell Ritchie wrote:You can get a Stream from a List very easily. But it only works in Java8.
myList.stream().forEach(c -> System.out.println(c)).


For just "for each" (without filtering and other useful intermediate operations) you don't have to use streams. List interface defines forEach method, so this might look like:
myList.forEach(c -> System.out.println(c));
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote: . . .
But why not double type?
. . .
When he is setting the price field?

I hadn't realise there was a forEach method in Lists now; that is an improvement on what I wrote. I see that method is inherited from Iterable.
I presume you can shorten that code with a method reference
myList.forEach(System.out::println);
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:When he is setting the price field?

I have not paid attention to what this field is. I only noticed float and thought Why not double?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!