Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2008, 01:44 AM
Member
 
Join Date: Mar 2008
Posts: 2
theonlywalks is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-14-2008, 04:21 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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 + "]"; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-15-2008, 07:15 AM
Member
 
Join Date: Mar 2008
Posts: 2
theonlywalks is on a distinguished road
thankyou
thankyou very much for your help!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Foreach on an Array Java Tip java.lang 0 04-17-2008 12:06 AM
foreach in jstl to display nested colums-struts jsp adil7 JavaServer Pages (JSP) and JSTL 1 02-11-2008 07:19 AM
While loop leebee New To Java 1 07-18-2007 04:11 PM
How to use do.. while loop -JaVa- New To Java 1 06-30-2007 08:08 PM
Question mark colon operator question orchid Advanced Java 3 04-30-2007 11:37 PM


All times are GMT +3. The time now is 07:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org