- import java.util.ArrayList;
- import java.util.List;
- public class GetSubListOfJavaArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- arrayList.add("4");
- arrayList.add("5");
- /*
- To get a sub list of Java ArrayList use
- List subList(int startIndex, int endIndex) method.
- This method returns an object of type List containing elements from
- startIndex to endIndex - 1.
- */
- List lst = arrayList.subList(1,3);
- //display elements of sub list.
- System.out.println("Sub list contains : ");
- for(int i=0; i< lst.size() ; i++)
- System.out.println(lst.get(i));
- /*
- Sub List returned by subList method is backed by original Arraylist. So any
- changes made to sub list will also be REFLECTED in the original Arraylist.
- */
- //remove one element from sub list
- Object obj = lst.remove(0);
- System.out.println(obj + " is removed from sub list");
- //print original ArrayList
- System.out.println("After removing " + obj + " from sub list, original ArrayList contains : ");
- for(int i=0; i< arrayList.size() ; i++)
- System.out.println(arrayList.get(i));
- }
- }
Thursday, 29 November 2012
Get Sub List of Java ArrayList Example
Sort Element Of ArrayList In Java
- import java.util.ArrayList;
- import java.util.Collections;
- public class SortJavaArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("3");
- arrayList.add("5");
- arrayList.add("2");
- arrayList.add("4");
- /*
- To sort an ArrayList object, use Collection.sort method. This is a
- static method. It sorts an ArrayList object's elements into ascending order.
- */
- Collections.sort(arrayList);
- //display elements of ArrayList
- System.out.println("ArrayList elements after sorting in ascending order : ");
- for(int i=0; i<arrayList.size(); i++)
- System.out.println(arrayList.get(i));
- }
- }
Search an element from ArrayList
import java.util.ArrayList;
public class SearchElementInArrayList
{
public static void main(String args[])
{
ArrayList arrList=new ArrayList();
arrList.add("1");
arrList.add("2");
arrList.add("3");
Boolean flag=arrList.contains("1");
System.out.println("Contains?= "+flag);
int indexNum=arrList.indexOf("1");
System.out.println("Index= "+indexNum);
}
}
public class SearchElementInArrayList
{
public static void main(String args[])
{
ArrayList arrList=new ArrayList();
arrList.add("1");
arrList.add("2");
arrList.add("3");
Boolean flag=arrList.contains("1");
System.out.println("Contains?= "+flag);
int indexNum=arrList.indexOf("1");
System.out.println("Index= "+indexNum);
}
}
Replace an element in arraylist
import java.util.ArrayList;
public class ReplaceElementInArrayList
{
public static void main(String args[])
{
ArrayList arrList=new ArrayList();
arrList.add("Harit");
arrList.add("Lalit");
for(Object list:arrList)
{
System.out.println(list);
}
arrList.set(0,"Nikki");
for(Object list:arrList)
{
System.out.println(list);
}
}
}
public class ReplaceElementInArrayList
{
public static void main(String args[])
{
ArrayList arrList=new ArrayList();
arrList.add("Harit");
arrList.add("Lalit");
for(Object list:arrList)
{
System.out.println(list);
}
arrList.set(0,"Nikki");
for(Object list:arrList)
{
System.out.println(list);
}
}
}
Remove an element from specified index of Java ArrayList Example
- import java.util.ArrayList;
- public class RemoveElementFromArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- /*
- To remove an element from the specified index of ArrayList use
- Object remove(int index) method.
- It returns the element that was removed from the ArrayList.
- */
- Object obj = arrayList.remove(1);
- System.out.println(obj + " is removed from ArrayList");
- System.out.println("ArrayList contains...");
- //display elements of ArrayList
- for(int index=0; index < arrayList.size(); index++)
- System.out.println(arrayList.get(index));
- }
- }
Remove all elements from Java ArrayList Example
- import java.util.ArrayList;
- public class RemoveAllElementsOfArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- System.out.println("Size of ArrayList before removing elements : "
- + arrayList.size());
- /*
- To remove all elements from the ArrayList use
- void clear() method.
- */
- arrayList.clear();
- System.out.println("Size of ArrayList after removing elements : "
- + arrayList.size());
- }
- }
Iterate a ArrayList Using Iterator
- import java.util.ArrayList;
- import java.util.Iterator;
- public class IterateThroughArrayListUsingIteratorExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- arrayList.add("4");
- arrayList.add("5");
- //get an Iterator object for ArrayList using iterator() method.
- Iterator itr = arrayList.iterator();
- System.out.println("Iterating through ArrayList elements...");
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
Insert all elements of other Collection to Specified Index of Java ArrayList Example
- import java.util.ArrayList;
- import java.util.Vector;
- public class InsertAllElementsOfOtherCollectionToArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- //create a new Vector object
- Vector v = new Vector();
- v.add("4");
- v.add("5");
- //insert all elements of Vector to ArrayList at index 1
- arrayList.addAll(1,v);
- //display elements of ArrayList
- System.out.println("After inserting all elements of Vector at index 1,
- ArrayList contains..");
- for(int i=0; i<arrayList.size(); i++)
- System.out.println(arrayList.get(i));
- }
- }
Wednesday, 28 November 2012
Copy all elements of Java ArrayList to an Object Array Example
mport java.util.ArrayList;
public class CopyElementsOfArrayListToArrayExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to ArrayList
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/*
To copy all elements of java ArrayList object into array use
Object[] toArray() method.
*/
Object[] objArray = arrayList.toArray();
//display contents of Object array
System.out.println("ArrayList elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
}
}
public class CopyElementsOfArrayListToArrayExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to ArrayList
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/*
To copy all elements of java ArrayList object into array use
Object[] toArray() method.
*/
Object[] objArray = arrayList.toArray();
//display contents of Object array
System.out.println("ArrayList elements are copied into an Array.
Now Array Contains..");
for(int index=0; index < objArray.length ; index++)
System.out.println(objArray[index]);
}
}
Append all elements of other Collection to Java ArrayList Example
import java.util.ArrayList;
import java.util.Vector;
public class AppendAllElementsOfOtherCollectionToArrayListExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
//create a new Vector object
Vector v = new Vector();
v.add("4");
v.add("5");
//append all elements of Vector to ArrayList
arrayList.addAll(v);
//display elements of ArrayList
System.out.println("After appending all elements of Vector,ArrayList contains..");
for(int i=0; i<arrayList.size(); i++)
System.out.println(arrayList.get(i));
}
}
Array List
ArrayList
Java ArrayList is a resizable array which implements List interface. ArrayList provides all operation defined by List interface. Internally ArrayList uses an array to store its elements. ArrayList provides additional methods to manipulate the array that actually stores the elements.
ArrayList is equivalent to Vector, but ArrayList is not synchronized.
Java ArrayList Capacity
Capacity of an ArrayList is the size of the array used to store the list elements. It grows automatically as we add elements to it. Every time this happens, the internal array has to be reallocated. This increases the load.
We can set the initial capacity of the ArrayList using following method.
ArrayList arrayList = new ArrayList();
arrayList.ensureCapacity(100);
Java ArrayList Iterators
Java ArrayList provides two types of Iterators.
1) Iterator
2) ListIterator
Iterator iterator = arrayList.iterator();
Returns object of Iterator.
ListIterator listIterator = arrayList.listIterator();
Returns object of ListIterator.
ListIterator listIterator = arrayList.listIterator(int startIndex);
Returns object of ListIterator. The first next() method call on this ListIterator object will return the element at the specified index passed to get the ListIterator object.
Iterators returned by these methods are fail-fast. That means if the list is modified after getting the Iterator by using some other means rather than Iterators own add or remove method, Iterator will throw ConcurrentModificationException.
ArrayList Constructors
1) ArrayList()
Creates an empty ArrayList.
For example,
ArrayList arrayList = new ArrayList();
2) ArrayList(int capacity)
Creates an ArrayList with specified initial capacity.
For example,
ArrayList arrayList = new ArrayList(10);
3) ArrayList(Collection c)
Creates an ArrayList containing elements of the collection specified.
For example,
ArrayList arrayList = new ArrayList(myCollection);
Where myCollection is an object of the type Collection. This creates an ArrayList of elements contained in the myCollection, in the order returned by the myCollection’s Iterator.
Tuesday, 20 November 2012
Add or substract hours to current time using Java Calendar
- /*
- Add or substract hours to current time using Java Calendar
- This example shows how to add or substract hours in current time
- using Java Calendar class.
- */
- import java.util.Calendar;
- public class AddHoursToCurrentDate {
- public static void main(String[] args) {
- //create Calendar instance
- Calendar now = Calendar.getInstance();
- System.out.println("Current Date : " + (now.get(Calendar.MONTH) + 1)
- + "-"
- + now.get(Calendar.DATE)
- + "-"
- + now.get(Calendar.YEAR));
- System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY)
- + ":"
- + now.get(Calendar.MINUTE)
- + ":"
- + now.get(Calendar.SECOND));
- //add hours to current date using Calendar.add method
- now.add(Calendar.HOUR,10);
- System.out.println("New time after adding 10 hours : "
- + now.get(Calendar.HOUR_OF_DAY)
- + ":"
- + now.get(Calendar.MINUTE)
- + ":"
- + now.get(Calendar.SECOND));
- /*
- * Java Calendar class automatically adjust the date accordingly if adding
- * hours to the current time causes current date to be changed.
- */
- System.out.println("New date after adding 10 hours : "
- + (now.get(Calendar.MONTH) + 1)
- + "-"
- + now.get(Calendar.DATE)
- + "-"
- + now.get(Calendar.YEAR));
- //substract hours from current date using Calendar.add method
- now = Calendar.getInstance();
- now.add(Calendar.HOUR, -3);
- System.out.println("Time before 3 hours : " + now.get(Calendar.HOUR_OF_DAY)
- + ":"
- + now.get(Calendar.MINUTE)
- + ":"
- + now.get(Calendar.SECOND));
- }
- }
- /*
- Typical output would be
- Current Date : 12-25-2007
- Current time : 18:31:42
- New time after adding 10 hours : 4:31:42
- New date after adding 10 hours : 12-26-2007
- Time before 3 hours : 15:31:42
- */
Add or substract days to current date using Java Calendar
- /*
- Add or substract days to current date using Java Calendar
- This example shows how to add or substract days in current date and time values
- using Java Calendar class.
- */
- import java.util.Calendar;
- public class AddDaysToCurrentDate {
- public static void main(String[] args) {
- //create Calendar instance
- Calendar now = Calendar.getInstance();
- System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
- + "-"
- + now.get(Calendar.DATE)
- + "-"
- + now.get(Calendar.YEAR));
- //add days to current date using Calendar.add method
- now.add(Calendar.DATE,1);
- System.out.println("date after one day : " + (now.get(Calendar.MONTH) + 1)
- + "-"
- + now.get(Calendar.DATE)
- + "-"
- + now.get(Calendar.YEAR));
- //substract days from current date using Calendar.add method
- now = Calendar.getInstance();
- now.add(Calendar.DATE, -10);
- System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1)
- + "-"
- + now.get(Calendar.DATE)
- + "-"
- + now.get(Calendar.YEAR));
- }
- }
- /*
- Typical output would be
- Current date : 12-25-2007
- date after one day : 12-26-2007
- date before 10 days : 12-15-2007
- */