Results 1 to 6 of 6
  1. #1
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Question Assistance on my code. Do not understand Java's "for each" for loop?

    Hi everyone,
    I am new to Java, and I working on this assignment but one of the instruction asks "if list is of type Node<T>, you should be able to iterate over list using
    Java’s “for each” for-loop:
    for (T e : list) { /* do something with e */ }" and I am lost at the "for each for loop" what does it mean?, and I tried to implemented it below in my code, it is commented out.
    Would that be a correct way of doing it? Thanks.



    Java Code:
    import java.util.Iterator;
    
    
    public final class Node<T> implements Iterable<T> {
    	
    	public final T v;
    	public Node<T> next;
    	
    	public Node (T val, Node<T> link) 
    	{
    		v = val; 
    		next = link;
    	
    	}
    	
    	//For each?
    	/* for (T e : list){
    		Iterator<T> It = this.iterator();
    		while(It.hasNext()){
    			e.accept(It.next());
    		}
    		
    	}
    	 */
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Assistance on my code. Do not understand Java's "for each" for loop?

    Java Code:
    for (TheType aTheTypeVar : listOfTheTypes){
    The for each loop removes the next TheType object from the listOfTheTypes list and stores it in the variable: aTheTypeVar
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Assistance on my code. Do not understand Java's "for each" for loop?

    Ah I see.
    If I wanted to make a main() to test my NodeIterator class, how would I construct it?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Assistance on my code. Do not understand Java's "for each" for loop?

    Usually to test a class and its methods, you create an instance of the class and call its methods.
    Java Code:
       TheClass anIns = new TheClass(<args?>);  // create an instance
       anIns.method1(<args?>);                        // call method 
       etc
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Assistance on my code. Do not understand Java's "for each" for loop?

    Ah, thank you very much.

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: Assistance on my code. Do not understand Java's "for each" for loop?

    just for what it's worth: Java8 introduced a method that is actually called forEach() and does the same thing:

    list.forEach(e -> doSomethingWithE(e))

Similar Threads

  1. Need help to understand the keyword "this" in this code.
    By CrushingJava in forum New To Java
    Replies: 1
    Last Post: 06-08-2013, 07:31 PM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Replies: 18
    Last Post: 04-03-2012, 09:49 AM
  4. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •