Tuesday, March 18, 2014

Notes on Java Common Code

Convert ArrayList To Arrays In Java
http://viralpatel.net/blogs/convert-arraylist-to-arrays-in-java/
String [] countries = list.toArray(new String[list.size()]);

Convert Array to ArrayList
tring[] countries = {"India", "Switzerland", "Italy", "France"};
List list = Arrays.asList(countries);
System.out.println("ArrayList of Countries:" + list);
The above code will work great. But list object is immutable. Thus you will not be able to add new values to it. In case you try to add new value to list, it will throw UnsupportedOperationException.

If you want to create a mutable list:
List list = new ArrayList(Arrays.asList(countries));

Java DOM getNodeValue() and getTextContent()
http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
http://stackoverflow.com/questions/5527195/java-dom-gettextcontent-issue
The Node#getTextContext method returns the text content of the current node and its descendants. Use *node.getFirstChild().getNodeValue()*, which prints out the text content for your node and not its descendants. 

The javadoc for Node defines "getNodeValue()" to return null for Nodes of type Element. 

No comments:

Post a Comment