Fixing “emulator: ERROR: unknown virtual device name:” issue with Android SDK
Android SDK expects your AVD to be located at the location “C:\Documents and Settings\<username>”. If your home folder is at some other location, you might see the error:
ERROR: unknown virtual device name: <device name>
when you try to run your application in the emulator. To fix this, create a new environment variable named “ANDROID_SDK_HOME” and set its value to the location of your home folder. To be sure that you are entering the correct value, verify that there’s a folder named “.android” at this location.
onclick or ONCLICK or onClick?
HTML is case-insensitive and therefore the casing of event handler names in your HTML documents is irrelevant. E.g. you can use onclick, ONCLICK, onClick or event oNcLiCk for your event handler name. However, XHTML is not so liberal and stipulates that all event handler names should be lowercase (e.g. onclick=”…”). Therefore, to make your HTML pages XHTML compliant, always use lowercase event handler names.
Using StringReader to iterate over a String
StringReader makes it quite convenient to iterate over a string character by character as follows:
StringReader reader = new StringReader("This is a string.");
int ch;
while((ch = reader.read()) != -1)
{
System.out.println((char)ch);
}
Creating singleton collections
The Collections class in the java.util package provides convenient methods to create singleton collection objects. A singleton collection object is a List, Map or Set that contains only a single element. Here’s how you can create a singleton set:
Set set = Collections.singleton( "The Element" );
There are 2 more methods which allow you to create a singleton list or map:
List list = Collections.singletonList( "The Element" );
Map map = Collections.singletonMap( "Key" , "Value" );
The collection object returned in all these methods is an immutable object. Also, all 3 of them are static methods in the Collections class.
Creating unmodifiable & synchronized collections
By default, the classes available in the Java Collections framework are read/write enabled & not thread safe. Sometimes the requirement is to have read only i.e. unmodifiable collections. The Collections class available in the java.util package provides utility methods to create such collections. Note: Do not confuse this class with the Collection interface which is the root interface of the collection hierarchy.
The objects returned by these methods are wrappers over the original collection object you supply to the method (Decorator design pattern).
The Collections class has 6 static methods that return a read only collection object. These are:
It is important that you don’t store the reference to the original object, otherwise you’ll still be able to access the original object and thus the collection will not be unmodifiable. Therefore, it is best to first populate the collection with the desired values and then replace the original reference with the read-only reference e.g.
List list = new Arraylist();
// populate the list
list = Collections.unmodifiableList(list);
The classes available in the collections package are not thread safe by default. The designers of the Java collections hierarchy did this on purpose since synchronized collection classes are slower and you don’t require synchronization always. The Collections class has 6 utility static methods to wrap collection objects into synchronized ones. These methods are:
It is best to create a collection object and then wrap it in a synchronized one immediately. Also, you should not keep the reference to the original object otherwise you’ll be able to access it in a thread-unsafe manner. The best way to do this is to create a synchronized collection object as follows:
List list = Collections.synchronizedList(new ArrayList());
