Amit | Techbirds

ArrayList is a basic implementation of List interface in Collection framework.It extends AbstractList. ArrayList supports dynamic array that can grow as needed.

Some of the important methods in ArrayList are

void add(int index,Object element):add the element at the specified index. void add(Object element): Add the element at the end of the list. void  clear(): clears the list when called on the list.

Object remove(int index): Remove the element at the specified location

We need an iterator to iterate over the list. ArrayList iterator is fail-fast by design.That means as soon as the underlying data structure changes after creating iterator it will throw java.util.ConcurrentModificationException.

Internally when we create an array list, it will maintains a modCount variable which will keep track of the modification count and every time we use add, remove or trimToSize method, it increments. expectedModCount is the iterator variable that is initialized when we create iterator with same value as modCount. This explains why we don’t get exception if we use set method to replace any existing element.

So basically iterator throws ConcurrentModificationException if list size is changed.

Let’s run the program to see 

Exception in thread “main” java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification( at java.util.AbstractList$

at misc.CopyOnArrayListExample.main(

As you can see, ArrayList iterator doesn’t allow concurrent modification when we iterating.If we need to do concurrent modification then we need  CopyOnWriteArrayList.

Lets see CopyOnWriteArrayList in action.

In above code, we just changed the class definition from ArrayList to CopyOnWriteArrayList and see the output below.

James Gosling Brendan Eich Rod Johnson Gavin King

As you can see, the above code doesn’t throw any exception as this one is thread safe variant of ArrayList.

Few things about CopyOnWriteArrayList from official documentation

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.
This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don’t want to synchronize traversals, yet need to preclude interference among concurrent threads. The “snapshot” style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

All elements are permitted, including null.

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread.

31 total views, no views today

I always try to bring some thing new and useful on this blog. This time we will understand the Void.class (which in itself looks something tricky) present in rt.jar.

One can consider the java.lang.Void class as a wrapper for the keyword void.

Some developers draw the analogy with the primitive data types int, long, short and byte etc. which have the wrapper classes as Integer, Long, Short and Byte receptively. But it should be kept in mind that unlike those wrappers Void class doesn’t store a value of type void in itself and hence is not a wrapper in true essence.


The Void class according to javadoc exists because of the fact that some time we may need to represent the void keyword as an object. But at the same point we cannot create an instance of the Void class using the new operator.

This is because the constructor in Void has been declared as private. 
Moreover the Void class is a final class which means that there is no way we can inherit this class.

So the only purpose that remains for the existence of the Void class is reflection, where we can get the return type of a method as void. The following piece of code will demonstrate this purpose:

public class Test { public static void main(String[] args) throws SecurityException, NoSuchMethodException { Class c1 = Test1.class.getMethod(“Test1”,null).getReturnType(); System.out.println(c1 == Void.TYPE); System.out.println(c1 == Void.class); } } class Test1{ public void Test1(){} }

  public static void main(String[] args) throws SecurityException, NoSuchMethodException {     Class c1 = Test1.class.getMethod(“Test1”,null).getReturnType();     System.out.println(c1 == Void.TYPE);     System.out.println(c1 == Void.class);

One can also use Void class in Generics to specify that you don’t care about the specific type of object being used. For example:

You’d use it mostly in a callback situation where you’re not actually returning anything but still need to specify a type because it might be used in a case where a type is returned. i.e.:

new Callback() { public void onSuccess(Void myResult) { //myResult == null } }

    public void onSuccess(Void myResult) {

You can’t use new to make a Void, but you can make one through setting it to null:

26 total views, no views today

Hi all this is a common error I think everyone might have faced. While  Help -> install new software  you sometimes get reported by eclipse   Missing required feature:  In this case you should check  Contact all sites  checkbox.

But do not always turn it on if your app while installing gives error  could not find repository  then you have to turn it off.

35 total views, 1 views today

Hi all,

I recently did a POC on facebook login. Here is my workaround.

I used the Loginbutton Widget to login.

first of all in linearlayout put this

xmlns:android=”” xmlns:fb=”” android:layout_width=”match_parent” android:layout_height=”wrap_content”>     

Then prefer using UiLifeCycleHelper as provided by Tapan

In that you will find onSessionStateChanged method with three parameters

Session State FacebookException

Now your Facebook Login is ready to use. In case you could not login check the exception in debug.

61 total views, no views today

Steps to record video are

mCamera = mPreview.getCamera(); //get Camera Intance mRecorder = new MediaRecorder(); //step1 mCamera.unlock(); mRecorder.setCamera(mCamera); //step2 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //step 3 //mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); //step4 mRecorder.setOutputFile(getOutputFile(MEDIA_TYPE_VIDEO).toString()); //step 5 mRecorder.setPreviewDisplay(mPreview.getMyHolder().getSurface()); try { //step 6 mRecorder.prepare(); Thread.sleep(1000); mRecorder.start(); Toast.makeText(CameraPreview.this, “Recording Started”, Toast.LENGTH_LONG); } catch (Exception e) { releaseRecorder(); Toast.makeText(CameraPreview.this, “Recording error couldn’t start”, Toast.LENGTH_LONG); }

mCamera = mPreview.getCamera(); //get Camera Intance mRecorder = new MediaRecorder(); mRecorder.setCamera(mCamera); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); mRecorder.setOutputFile(getOutputFile(MEDIA_TYPE_VIDEO).toString()); mRecorder.setPreviewDisplay(mPreview.getMyHolder().getSurface());     Toast.makeText(CameraPreview.this, “Recording Started”, Toast.LENGTH_LONG);     Toast.makeText(CameraPreview.this, “Recording error couldn’t start”, Toast.LENGTH_LONG);

56 total views, no views today