Skip to content

Instantly share code, notes, and snippets.

@jberkel
Created April 6, 2011 10:22
Show Gist options
  • Save jberkel/905438 to your computer and use it in GitHub Desktop.
Save jberkel/905438 to your computer and use it in GitHub Desktop.
daily WTF
public static String join(List<?> list, String delim) {
StringBuilder buf = new StringBuilder();
int num = list.size();
for (int i = 0; i < num; i++) {
if (i != 0)
buf.append(delim);
if (list.get(i) instanceof String)
buf.append(list.get(i));
else if (list.get(i).getClass() == Integer.TYPE || list.get(i).getClass() == Integer.class)
buf.append(Integer.toString(((Integer) list.get(i))));
}
return buf.toString();
}
@robb
Copy link

robb commented Apr 6, 2011

What’s the WTF? Not calling toString() on everything?

@jberkel
Copy link
Author

jberkel commented Apr 6, 2011

yes, and also casting to Integer and then passing it to Integer.toString(). object oriented programming, we can haz it. The other wtf is that Android already has support for it (TextUtils)

@robb
Copy link

robb commented Apr 6, 2011

but that cast has to be in there because list.get() returns an Object without a better generic, doesn’t it?
edit: which is then coerced to the primitive because Integer.toString() expects an int… omg

At least he was using a StringBuilder…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment