Created
February 4, 2015 21:01
-
-
Save bharath2020/9f156d8f6e6dfe719552 to your computer and use it in GitHub Desktop.
Synchronized array in JAVA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
/** | |
* Created by bbooshan on 2/3/15. | |
*/ | |
public class FTSynchronizedArray<T> { | |
private ArrayList<T> itemStore; | |
public FTSynchronizedArray() | |
{ | |
itemStore = new ArrayList<T>(); | |
} | |
public synchronized void addItem(T item) | |
{ | |
itemStore.add(item); | |
} | |
public synchronized void clearAll() | |
{ | |
itemStore.clear(); | |
} | |
public synchronized void swapItems(int index1, int index2) | |
{ | |
int totalItems = itemStore.size(); | |
if( index1 < totalItems && index2 < totalItems) | |
{ | |
T temp = itemStore.get(index1); | |
itemStore.set(index1, itemStore.get(index2)); | |
itemStore.set(index2, temp); | |
} | |
} | |
public synchronized T getItem(int index) | |
{ | |
int totalItems = itemStore.size(); | |
T item = null; | |
if( index < totalItems) | |
{ | |
item = itemStore.get(index); | |
} | |
return item; | |
} | |
public synchronized int totalItems() | |
{ | |
return itemStore.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment