TreeSet size() Problem

R

Rhino

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
 
F

Fred

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
 
A

Anton Spaans

Rhino said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top