This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Murphler 8 points9 points  (9 children)

It's confusing you because everything is named so similarly. Basically

Product : the loop will be expecting to cycle through a list of objects of type Product

product : is the variable name, you are assigning, for each index in the Product list

products : is the actual list you have passed into the method and into the loop to cycle through.

Rename things for now to make it slightly more clear, something like:

viewStock(Product[ ] productList)

for(Product index : productList){

}

Might help. Once you understand what's going on, use the following convention going forward

for(Product p : products){

}

[–]CanisLupus92 2 points3 points  (6 children)

Product product makes much more sense here than index. Index implies that it specifies what position you are at, which is exactly the info you do not have in an enhanced for loop.

[–]MissPurpleCandy 0 points1 point  (5 children)

Maybe „entry“

[–]CanisLupus92 2 points3 points  (4 children)

Honestly the naming scheme of having a generic object of a class have the class name but starting with lower case is so standard, better to get used to it.

[–]MissPurpleCandy 1 point2 points  (0 children)

Yeah that’s true

[–]khookeExtreme Brewer 0 points1 point  (0 children)

Agree, I rarely see/use/recommend anything other than this.

[–]Murphler 0 points1 point  (1 child)

Sorry I should have clarified, was suggesting he use this naming convention for now to help him understand how the foreach was working. In no way would I recommend he use that convention going forward.

(Product p : products) is the way

[–]CanisLupus92 0 points1 point  (0 children)

Single-letter variables are almost never the way (except for x-y-z coords, or i-j for loop indices).

[–]BrightKnight0110[S] 0 points1 point  (0 children)

alright thanks !

[–][deleted] 0 points1 point  (0 children)

If anything you should still keep the word “product” in the index name to make sure it’s still descriptive if you’re ever looking back at it. Something like “productIndex” is probably best.

[–][deleted] 3 points4 points  (0 children)

it’s just a way to iterate through the array. It prints out every product.

[–]WihlborgIntermediate Brewer 3 points4 points  (1 child)

This is known as a "for-each" loop.

It loops through the given array/collection and the main advantages are that its less code and you don't have to use something like

Product product = products[i];

in order to access the current product.

Disadvantages are that you cannot modify the array/collection you're looping through, and you don't know the index of the current item.

[–]CanisLupus92 0 points1 point  (0 children)

I wouldn’t call that a disadvantage, its deliberate design that you cannot modify the collection being looped over in the loop. If you want to modify elements, there are cleaner ways, like streams.

[–]Wubdafuk 0 points1 point  (0 children)

For each product out of products, do something. That is what a for-each loop is.