Java doesn't deal with lists fluently.
All I'm doing here is splitting a comma-separated string, adding MY_NAME and joining. In Java:
List list = new ArrayList();
list.addAll(Arrays.asList(original.split(",")));
list.add(MY_NAME);
StringBuffer newString = new StringBuffer();
for (int i = 0; i < list.size(); i++) {
newString.append(list.get(i));
if (i+1 < list.size())
newString.append(',');
}
In Ruby:
original.split(",").push(MY_NAME).join(",")
Back to Java again... because this beautiful listcentric pattern isn't supported, I end up using the below code. It's less DRY and the intention is obscured.
String newString;
if (original.length() > 0)
newString = original.trim() +","+ MY_NAME;
else newString = MY_NAME;
No comments:
Post a Comment