I have a Project class that holds a name and various other attributes. Everytime a new Project is made it is added to an ArrayList.
ArrayList<Project> mProject = new ArrayList<Project>();
I have most of the code in a different question. I don’t feel like I should ask it there as it has a different question on it. But if you want to check out more of the code it’s here:
Can I use a method from a different class that I have added to an ArrayList?
And here is where I’m stuck. This is the first time I’ve used this type of loop before and I don’t know what’s going on. I have seen tutorials just pass over this loop with a “you should know what happens here” attituded. So if you could explain this loop that would be great also.
And on to the code:
System.out.println("Select one of the fallowing projects:");
for (Project proj : mProject){
int i= 1;
i++;
System.out.println( i + ": " + proj.returnName());
}
My output is this:
Select one of the fallowing projects:
2: Greg
2: Mike
It looks like it’s not going though the code. So I’d love an explanation or a tutorial with this loop. Thanks.
If I use a regular loop how whould I go about using the methods from the mProject arraylist?
for ( int i = 0; i<= mProject.size() ; i++){
Project proj = mProject;// This won't compile.
if (choice == proj.returnName()){
}
}
If you move the int declaration out of the loop it will increment as you would expect, it is getting reassigned every time: