TreeSet size() Problem

Discussion in 'Java' started by Rhino, Feb 21, 2005.

  1. Rhino

    Rhino Guest

    I am trying to subclass TreeSet to hold a Set of Strings. However, I'm
    having some problems with the size() method.

    Given this class definition:

    public class StringSet extends TreeSet {

    String[] sexes = new String[] {"M", "F"};

    public StringSet(String[] values) {

    stringSet = new TreeSet();
    for (int ix=0; ix<values.length; ix++) {
    stringSet.add(values[ix]);
    }

    System.out.println("StringSet(): The StringSet contains " +
    stringSet.size() + " strings.");
    }
    }

    and this code:

    String[] sexes = new String[] {"M", "F"};

    StringSet sexesSet = new StringSet(sexes);
    System.out.println("setup(): sexesSet.size() = " + sexesSet.size());

    I'm finding that the println() within the StringSet constructor correctly
    reports that there are two elements in the StringSet but the println() that
    follows the instantiation of the StringSet incorrectly reports a size of 0.

    I can't see a mistake in my code but I'm doubtful that there would be a bug
    in Java that causes the StringSet size to be misreported. Can anyone see
    what is wrong here?

    --
    Rhino
    ---
    rhino1 AT sympatico DOT ca
    "There are two ways of constructing a software design. One way is to make it
    so simple that there are obviously no deficiencies. And the other way is to
    make it so complicated that there are no obvious deficiencies." - C.A.R.
    Hoare
     
    Rhino, Feb 21, 2005
    #1
    1. Advertising

  2. Rhino

    Fred Guest

    So it looks like the variable "stringSet" is local only to the
    constructor (although is declaration is omitted from your posting). If
    this is, in fact, the case, then you're querying the size of two
    different variables in your sample code: The first query examines the
    local variable inside the StringSet; the second query examines the
    StringSet itself.

    I suggest investigating using a TreeSet< String > vs. subclassing
    TreeSet. This might be easier to do.

    -Fred
     
    Fred, Feb 21, 2005
    #2
    1. Advertising

  3. Rhino

    Anton Spaans Guest

    "Rhino" <> wrote in message
    news:xfaSd.2088$...
    > I am trying to subclass TreeSet to hold a Set of Strings. However, I'm
    > having some problems with the size() method.
    >
    > Given this class definition:
    >
    > public class StringSet extends TreeSet {
    >
    > String[] sexes = new String[] {"M", "F"};
    >
    > public StringSet(String[] values) {
    >
    > stringSet = new TreeSet();
    > for (int ix=0; ix<values.length; ix++) {
    > stringSet.add(values[ix]);
    > }
    >
    > System.out.println("StringSet(): The StringSet contains " +
    > stringSet.size() + " strings.");
    > }
    > }
    >
    > and this code:
    >
    > String[] sexes = new String[] {"M", "F"};
    >
    > StringSet sexesSet = new StringSet(sexes);
    > System.out.println("setup(): sexesSet.size() = " + sexesSet.size());
    >
    > I'm finding that the println() within the StringSet constructor correctly
    > reports that there are two elements in the StringSet but the println()

    that
    > follows the instantiation of the StringSet incorrectly reports a size of

    0.
    >
    > I can't see a mistake in my code but I'm doubtful that there would be a

    bug
    > in Java that causes the StringSet size to be misreported. Can anyone see
    > what is wrong here?
    >
    > --
    > Rhino
    > ---
    > rhino1 AT sympatico DOT ca
    > "There are two ways of constructing a software design. One way is to make

    it
    > so simple that there are obviously no deficiencies. And the other way is

    to
    > make it so complicated that there are no obvious deficiencies." - C.A.R.
    > Hoare
    >
    >


    Change your StringSet code to this, replace 'stringSet' with the keyword
    'this':

    class StringSet extends TreeSet {

    public StringSet(String[] values) {

    for (int ix=0; ix<values.length; ix++) {
    this.add(values[ix]);
    }

    System.out.println("StringSet(): The StringSet contains " +
    this.size() + " strings.");
    }
    }

    (note, though, that the 'this' keyword can be omitted)
     
    Anton Spaans, Feb 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandip Chitale

    Re: JList customized with TreeSet

    Sandip Chitale, Aug 23, 2003, in forum: Java
    Replies:
    0
    Views:
    679
    Sandip Chitale
    Aug 23, 2003
  2. Rhino
    Replies:
    17
    Views:
    1,058
    Rhino
    Feb 24, 2005
  3. Stefan Ram

    Re: correct use of TreeSet

    Stefan Ram, Feb 26, 2006, in forum: Java
    Replies:
    2
    Views:
    431
    Stefan Schulz
    Feb 26, 2006
  4. jacksu

    TreeSet bug?

    jacksu, Jun 15, 2006, in forum: Java
    Replies:
    2
    Views:
    1,251
    jacksu
    Jun 15, 2006
  5. setar
    Replies:
    3
    Views:
    640
    setar
    Jan 4, 2007
Loading...

Share This Page