4 Replies Latest reply: Feb 4, 2008 3:47 PM by 807601 RSS

    ArrayList Gotcha?

    807601
      Is there an ArrayList "gotcha"? When I add my own objects into an ArrayList and then pull them back out I only see the last object.




      import java.util.*;
      
      public class TesterClass {
      
           public static void main(String[] args) {
                ArrayList groups = new ArrayList();
                
                for(int i=0;i<10;i++){
                     groups.add(new Integer(i));
                     System.out.println(groups.get(i));
                }
                          
                System.out.println();
                
                for(int i = 0;i<10;i++){
                     System.out.println(groups.get(i).toString());
                     
                }
                
                groups = new ArrayList();
                System.out.println();
                
                
                for(int i=0;i<10;i++){
                     Group grp = new Group();
                     grp.setGroupName(i+"");
                     groups.add(grp);
                     System.out.println("GroupName "+grp.getGroupName());
                }
                
                System.out.println();
                
                for(int i = 0;i<10;i++){
                     System.out.println("GroupName "+((Group)groups.get(i)).getGroupName());
                     
                }
                
                
                
           }
      
           
           private static class group{
                private static String groupName="";
      
                public static String getGroupName() {
                     return groupName;
                }
      
                public static void setGroupName(String groupName) {
                     group.groupName = groupName;
                }
                
           }
           
      }
        • 1. Re: ArrayList Gotcha?
          807601
          What's your actual code? And the actual ouput? I'm pretty sure this isn't the actual code since your private class is 'group' with a lower case g, and you create grp with an uppercase G 'Group'. So this code won't even compile
          • 2. Re: ArrayList Gotcha?
            807601
            Your class Group is facked up -- static data, puh-lease! Demo:
            import java.util.*;
            
            public class TesterClass {
                public static void main(String[] args) {
                    List<Group> groups = new ArrayList<Group>();
                    for(int i=0;i<10;i++){
                        groups.add(new Group(String.valueOf(i)));
                    }
                    System.out.println(groups);
                }
            
                private static class Group{
                    private String name;
            
                    public Group(String name) {
                        this.name = name;
                    }
            
                    public String toString() {
                        return name;
                    }
                }
            }
            • 3. Re: ArrayList Gotcha?
              807601
              Can I paramaterize the List with 1.4?
              • 4. Re: ArrayList Gotcha?
                807601
                snguyen wrote:
                Can I paramaterize the List with 1.4?
                No. What you should do with 1.4 is upgrade ;-)