Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TreeSet size() Problem

Reply
Thread Tools

TreeSet size() Problem

 
 
Rhino
Guest
Posts: n/a
 
      02-21-2005
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


 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      02-21-2005
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

 
Reply With Quote
 
 
 
 
Anton Spaans
Guest
Posts: n/a
 
      02-22-2005

"Rhino" <(E-Mail Removed)> wrote in message
newsfaSd.2088$(E-Mail Removed)...
> 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with searching in TreeSet expressions starting with a given text setar Java 3 01-04-2007 11:51 PM
TreeSet bug? jacksu Java 2 06-15-2006 09:47 PM
Re: correct use of TreeSet Stefan Ram Java 2 02-26-2006 03:47 PM
Why would an add() in a TreeSet subclass fail? Rhino Java 17 02-24-2005 05:09 AM
Re: JList customized with TreeSet Sandip Chitale Java 0 08-23-2003 09:44 PM



Advertisments