Archive for August 12th, 2004

The perils of split()

Can you spot why the following program:



public class Split {
public static void main(String[] argv) {
String STRING = "foo  bar";
String[] s = STRING.split(" ");
for (int i = 0; i < s.length; i++) {
System.out.println(i + " '" + s[i] + "'");
}
}
}

displays:

0 'foo'
1 ''
2 'bar'

The reason is that split() works a little differently from StringTokenizer: 
it accepts a regular expression as a separator.  In the code above, I
define this regular expression as " " (one space character) but the input string
contains two of them.  Therefore, we can solve this problem by using the "
+" as a regular expression ("at least one space character").

Still, the fact that split() can return empty strings is deceiving,
especially if you are converting your code from StringTokenizer.

There
are a couple of good things about this behavior, though:

  • You can reconstitute
    the original string if you need to.
  • It makes it easier to parse strings with records that can
    be empty, such as lines from a log file.

Can you think of any other use?

Eclipse plug-in for generics

I have just added support for concrete collections to
J15:  use Ctrl-1 on the creation of a concrete collection and J15 will add the
correct Generic parameters.

Before:

List l2 = new ArrayList();
Float n2 = new Float(42);
l2.add(n2);

After:

ArrayList<Float> l2 = new ArrayList<Float>();
Float n2 = new Float(42);
l2.add(n2);

Before:

Map m = new HashMap();
m.put(new Integer(42), "Cedric");

After:

HashMap<Integer, String> m = new HashMap<Integer, String>();
m.put(new Integer(42), "Cedric");

Let me know if you can think of other conversions that this plug-in could make
for you.