View Single Post
  #2 (permalink)  
Old 03-14-2008, 05:21 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
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 + "]"; } }
Reply With Quote