Saturday, July 11, 2009

Viral Patel gave a short discussion on his blog entitled Convert ArrayList to Arrays in Java. This is a good thing to know how to do but he did not cover the reverse operation, converting arrays to lists. It holds a few surprises for you. So here is a short snippet that demonstrates this opposite operation.

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class ArraysAsList {
public statis void main(String[] args) {
new ArraysAsList().start();
}

private void start() {
String[] countries = {"India", "Switzerland", "Italy", "France"};
List list = Arrays.asList(countries);
list.add("India");
}
}

If we run this small program we get.....
WHAT!!!! an exception?!?!?!What going on here? All we wanted to do was add an element to the generated array. Here's the exception:

java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:131)

Whaddaya mean unsupported operation? Let's look at the javadoc and see what it says:

"Returns a fixed-size list backed by the specified array."
Aha! there's the problem. That means that I can't remove anything either. Try it.
Don't forget this in your coding, it can bite you. Not a big deal but just what you need to deal with when you're under the pressure of a deadline.

Thanks to Damien for reminding me to show you the correct way to do this:
List list = Arrays.asList(countries);
should be:
List list = new ArrayList(Arrays.asList(countries));
This fixes the problem. Now you can add, delete, whatever.

Saturday, July 4, 2009

Get more kids into Computing, Alice.

Ok, I digress from technical topics again. This time though I think it's an important digression. Let's get more kids interested in computing!!
As of 2004 only 4.1% of all Bachelors degrees awarded were in Computer Science (that's the latest data the CRA had). In 2008, only 11.8 percent of U.S. bachelor's degrees in computer science went to women. This is another sore point but I'll make that a topic for another blog post someday. The point is we need to get more kids interested in computing!!
Many kids think that computing isn't fun (some programmers think the same thing) so we need to make it fun. Thus enters Alice. With support from the National Science Foundation, professor Susan Rodger of Duke University is hoping that Alice will get kids interested in computer science.
A ScienceDaily site gives an overview of the problem and discusses Alice at a high level. Basically, Alice is a 3D programming environment that makes creating animation for story telling easy. It is designed to be a first exposure to object-oriented programming and is available for Windows and Mac. According to the site, kids that have been introduced to Alice have a hard time stopping once they get started.
Yes, I know, maybe you consider this a rant and not a blog post but as I said earlier this is important if we are going to maintain a technological edge in computing. I can't believe I'm saying this but I'm thinking of trying to start a computing summer camp for younger children. I just have to fine the financial backing
(I don't believe I just said that?!?!) How about some other ideas?