开发者

Problem with my for loop. I have never used this type before and it's not working like a regular loop

开发者 https://www.devze.com 2023-03-14 22:26 出处:网络
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.

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 开发者_C百科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:

   int i= 1; 
    for (Project proj : mProject){
      i++;
      System.out.println( i + ": " + proj.returnName());}


your int Variable gets initalised with 1 in the for each loop, then incremented. In the next iteration, it gets initialised with 1 again, then incremented.

int i= 1;
System.out.println("Select one of the fallowing projects:");    
for (Project proj : mProject){

i++;
System.out.println( i + ": " + proj.returnName());
}

This should work properly


Its basically just a different way of writing a for loop, also known as a foreach loop.

http://download.oracle.com/javase/1,5.0/docs/guide/language/foreach.html

In your code example, the variable proj gets set to the next item in the list mProject. This happens until the list has nothing left to iterate through.

The variable i is local to the for loop, so it is being recreated each time, as others have said. In other words, every iteration of this loop will make i = 2, as you can see from your output.


See that you delcare the "int i = 1; " inside the loop.

So, every time the loop "loops", i gets the value 1 again.

It should be:

    int i =1;
    for(Project proj: mProject) {
    i++;
    System.out.println( i + ": " + proj.returnName());
    }

See the "int i = 1 ; " out of the loop ?


This type of loop is known as a foreach loop in Java. Basically, it's shorthand for saying, "for each item in this array/collection/iterable, do ..."

The shorthand is nice, but the downside, as you have run into, is that you don't have access to the index or iterator inside the loop when you use this form. So you should either separately track the index (like you're already trying to do, see Terrell's answer for the correct way), or use standard loop syntax, e.g. for(int i=0; i<mProject.length; i++)


for (Project proj : mProject){

    System.out.println( proj.returnName() );
}

means

For each Project in the Project Set mProject, which has been defined as:

{ GregProject, MikeProject }

Print out that project's returnName. proj is re-evaluated at each step of the loop, to point to the next Project in the Set.

0

精彩评论

暂无评论...
验证码 换一张
取 消