Monday, May 17, 2010

Use Arrays.asList() method with caution

Hey guys,

Been a while since the last post. Work has been hectic and i wasnt really in the mood to blog. My apologies. So getting along with it. I just got to know this interesting detail about java's Arrays.asList() method. If you go through the source code you will find out that it will not give you an implementation of the ArrayList class. Because it has an anonymous inner implmentation of the class ArrayList. And hence for example if you consider the following scenrion;

String[] strArr = new String[]{"test","test1};
List<String> strList = Arrays.asList(strArr);
strList.add("test3");

Here at line no 3 you will get an UnsupportedException thrown because that inner class implmentation does not provide all functionalities provided by the normal ArrayList class.

For more info check out this article.

No comments:

Post a Comment