static initialization of arrays



Hi guys,

I started to write some Java today, see the code below. What I want to
do is produce a static array V from several static arrays A, B, ...,
but I don't want V to have any duplicate elements. I was wondering if
there is a better way of coding this up? Also, is there a way to
guarantee that arrays A, B, ... are constructed before S and V without
relying on the order in which they are declared?

Thanks,

Johan

-----------------------------------------

import java.util.*;

class StringSet extends TreeSet<String>
{
public void addAll( String[] strings )
{
for (String s: strings )
{
add( s );
}
}
};

class Main
{
private static final String[] A = { "A1", "A2", "B1" };
private static final String[] B = { "B1", "B2" };
...

private static final StringSet S =
new StringSet ()
{
{
addAll( A );
addAll( B );
...
}
};

private static final String[] V = S.toArray(new String[S.size()]);

...
}

.



Relevant Pages

  • Re: static initialization of arrays
    ... do is produce a static array V from several static arrays A, B, ..., ... No need for a special StringSet class... ... public class MainWithCollection { ... You can use a static initializer (such ...
    (comp.lang.java.programmer)
  • Re: TreeSet size() Problem
    ... > public class StringSet extends TreeSet { ... > I'm finding that the printlnwithin the StringSet constructor correctly ... Change your StringSet code to this, replace 'stringSet' with the keyword ...
    (comp.lang.java.programmer)