The ArrayList class and the LinkedList class are concrete implementations of the List interface. Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection.

If, however, your application requires the insertion or deletion of elements from any place in the list, you should choose LinkedList. A list can grow or shrink dynamically. An array is fixed once it is created. If your application does not require insertion or deletion of elements, the most efficient data structure is the array.

java.util.ArrayList

ArrayList Example

public class TestList {
  public static void main(String[] args) {
    // Create a list
   ArrayList arrayList = new ArrayList();
   arrayList.add( new Integer(1) );
   arrayList.add( new Integer(2) );
   arrayList.add( new Integer(3) );
System.out.println("ArrayList contains " + arrayList.size() + " values");
if( arrayList.contains( new Integer(1) ) )
	System.out.println("ArrayList contains 1 as value");
else
	System.out.println("ArrayList does not contain 1 as value");
Integer one = (Integer) arrayList.get(0);
System.out.println("Value at 0 index is " + one);
System.out.println("Index of first occurrence of 1 is " + arrayList.indexOf( new Integer(1) ) );
System.out.println("Converting ArrayList to Object array");
Object[] elements = arrayList.toArray();
for(int i=0; i < elements.length ; i++)
       System.out.println(elements[i]);
System.out.println("Is 1 removed from the ArrayList ? " +
		arrayList.remove( new Integer(1) ));
  }
}

Output

      ArrayList contains 3 values
      ArrayList contains 1 as value
      Value at 0 index is 1
      Index of first occurrence of 1 is 0
      Converting ArrayList to Object array
      1
      2
      3
      Is 1 removed from the ArrayList ? true

java.util.LinkedList

Example: List

public class RemoveRangeElementsLinkedListExample {
 public static void main(String[] args) {
 //create LinkedList object
LinkedList lList = new LinkedList();

lList.add("1"); 		lList.add("2");
lList.add("3");		lList.add("4");
lList.add("5");

System.out.println("LinkedList contains : " + lList);

//remove elements from index 2(inclusive) to 5(exclusive)
lList.subList(2,5).clear();

System.out.println("Range of elements removed from LinkedList");
System.out.println("LinkedList now contains : " + lList);
}
}

LinkedList:Example 2

public class IterateThroughLinkedListUsingIteratorExample {
 public static void main(String[] args) {
 //create LinkedList object
LinkedList lList = new LinkedList();

lList.add("1");	lList.add("2");
lList.add("3");	lList.add("4");
lList.add("5");

Iterator itr = lList.iterator();

System.out.println("Iterating through elements of Java LinkedList...");
System.out.println("LinkedList contains : ");
while(itr.hasNext())
{
	System.out.println(itr.next());
}
}
}

Output

Iterating through elements of Java LinkedList...
LinkedList contains :
1
2
3
4
5

The Vector Class

Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If synchronization is required, you can use the synchronized versions of the collection classes.

Example:Vector

public class SearchAnElementFromSpecificIndexVectorExample {
 public static void main(String[] args) {
 Vector v = new Vector();
 v.add("1");	v.add("2");	v.add("3");
v.add("4");	v.add("5");	v.add("1");
v.add("2");
 int index = v.indexOf("1",4);
if(index == -1)
	System.out.println("Vector does not contain 1 after index # 4");
else
	System.out.println("Vector contains 1 after index # 4 at index #" + index);

int lastIndex = v.lastIndexOf("2" , 5);
if(lastIndex == -1)
	System.out.println("Vector does not contain 2 after index # 5");
else
	System.out.println("Last occurrence of 2 after index # 5 in Vector is at index #" + lastIndex);
 } }

Output

/*
 Output would be
 Vector contains 1 after index # 4 at index #5
 Last occurrence of 2 after index # 5 in Vector is at index #1
 */
Tagged with: JAVAObject Oriented
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.