Results 1 to 3 of 3
  1. #1
    theonlywalks is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default Question regarding foreach loop...

    Hey everyone, I'm kind of stuck in this foreach loop. heres my code:


    private Map<String, Person> record;

    public Group()
    {
    record - new TreeMap<String, Person>();
    }

    public Group(Group group)
    {
    record = new TreeMap<String, Person>();
    for (Person person : group.record)
    {
    this.record.put(person.getName(), person);
    }
    }

    Now as I understand the foreach loop...

    its taking all the elements that are in group.record and assigning them one by one to person. then im putting (person.getName(), and the actual object person into this.record.... however i keep getting the error "foreach not applicable to expression type" and its pointing at "group.record" in the foreach loop statement.

    i am really stuck with this... can someone please help me!! thankyou so much in advance.

    Shawn

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class GroupTest {
        private Map<String, Person> record;
    
        public GroupTest() {
            record = new TreeMap<String, Person>();
        }
    
        public GroupTest(GroupTest group) {
            record = new TreeMap<String, Person>();
            // The for-each construct works like an Iterator.
            // The right argument in the for-each construct
            // should be an array or List.
            // What is group.record?
            System.out.println("group.record = " +
                                group.record.getClass().getName());
            // Try using the keySet.
            for (String key : group.record.keySet()) {
                Person person = group.record.get(key);
                this.record.put(key, person);
            }
            // An alternative.
    //        record.putAll(group.record);
        }
    
        public static void main(String[] args) {
            String[] names = { "Sue", "John", "Linda" };
            GroupTest test = new GroupTest();
            for(String s : names)
                test.record.put(s, new Person(s));
            System.out.println("test = " + test.record);
            GroupTest defensiveCopy = new GroupTest(test);
            System.out.println("defensiveCopy = " + defensiveCopy.record);
        }
    }
    
    class Person {
        String name;
    
        Person(String name) { this.name = name; }
        public String getName() { return name; }
        public String toString() {
            return "Person[name:" + name + "]";
        }
    }

  3. #3
    theonlywalks is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. How to use do.. while loop
    By -JaVa- in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 08:52 PM
  3. How to use Foreach on an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:06 PM
  4. foreach in jstl to display nested colums-struts jsp
    By adil7 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-11-2008, 06:19 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 03:11 PM

Posting Permissions

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