J
Johan
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()]);
...
}
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()]);
...
}