Results 1 to 7 of 7
  1. #1
    Neivaed is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Adding to a HashSet

    Ok, I am using bluej and I am having trouble using a loop to add to a HashSet, as it just returns the size as null, saying there is nothing in it and I don't understand why.

    public Pack()
    {
    HashSet<Object> collection = new HashSet<Object>();
    for (int count = 0; count < 21)
    {
    collection.add(new Object());
    count++;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Neivaed View Post
    Ok, I am using bluej and I am having trouble using a loop to add to a HashSet, as it just returns the size as null, saying there is nothing in it and I don't understand why.

    Java Code:
    public Pack()
        {
            HashSet<Object> collection = new HashSet<Object>();
            for (int count = 0; count < 21)
            {   
                collection.add(new Object());
                count++;
            }
        }
    That code won't compile. Please only post real code that you've tested here. Also, you'll want to use code tags.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, compilable code will better show where your error is. This similar but compilable program has no problems:

    Java Code:
    import java.util.*;
    
    public class Foo {
       public static void main(String[] args) throws java.io.IOException {
          HashSet<Object> collection = new HashSet<Object>();
          for (int count = 0; count < 21; count++) {
             collection.add(new Object());
             //count++;
          }
          System.out.println("collection.size(): " + collection.size());
       }
    }

    So based on your questionable code, I have no idea what's wrong. If you are still having problems, again, post the real compilable code.

    Luck.

  4. #4
    Neivaed is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I'm really sorry >< I'm really not very good at this

    Java Code:
    import java.util.HashSet;
    
    /**
     * Write a description of class Pack here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Pack
    {
        private HashSet<Object> collection;
        
        /**
         * Constructor for objects of class Pocket
         */
        public Pack()
        {
            HashSet<Object> collection = new HashSet<Object>();
            for (int count=0; count < 21; count++)
            {   
                collection.add(new object());
            }
        }
    and basically Object is a different class

    Java Code:
    public class Object
    {   
        public Object()
        {
            
        }
    }
    Last edited by Neivaed; 12-05-2010 at 06:26 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are re-declaring your collection variable in the Pack constructor and are in effect shadowing the class collection field:

    Java Code:
    public class Pack
    {
        private HashSet<Object> collection;
        
        /**
         * Constructor for objects of class Pocket
         */
        public Pack()
        {
            // you are re-declaring collection here
            HashSet<Object> collection = new HashSet<Object>();
            for (int count=0; count < 21; count++)
            {   
                collection.add(new object());  [color="red"][b]// object with a lower-case o still compiles??[/b][/color]
            }
        }
    So instead don't re-declare the variable but rather in the constructor use the class field:

    Java Code:
        public Pack()
        {
            // HashSet<Object> collection = new HashSet<Object>();
            collection = new HashSet<Object>();

    I also hope that you really don't have a class named "Object".

  6. #6
    Neivaed is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    That makes sense, thank you very much.

    (No I don't actually have a class called Object)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. bluej hashset problem ;s
    By truant420 in forum Other IDEs
    Replies: 2
    Last Post: 12-07-2010, 06:56 PM
  2. :( anyone here plz help on HashSet
    By waklo99 in forum New To Java
    Replies: 8
    Last Post: 09-20-2010, 04:02 AM
  3. HashSet Contains problem
    By guywalder in forum Advanced Java
    Replies: 11
    Last Post: 09-01-2009, 03:48 PM
  4. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 09:22 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:34 PM

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
  •