Friday, October 31, 2014

Java Array List Pro



Perform Binary Search on Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class BinarySearchArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
Collections.sort(arrayList);
System.out.println("Sorted ArrayList contains : " + arrayList);
int index = Collections.binarySearch(arrayList,"4");
System.out.println("Element found at : " + index);
    }
}

Replace all occurrences of specified element of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class ReplaceAllArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("A");
arrayList.add("C");
arrayList.add("D");
System.out.println("ArrayList Contains : " + arrayList);
Collections.replaceAll(arrayList, "A","Replace All");
System.out.println("After Replace All, ArrayList Contains : " + arrayList);
    }
}
import java.util.ArrayList;
import java.util.Collections;
public class SwapElementsOfArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before swaping, ArrayList contains : " + arrayList);
Collections.swap(arrayList,0,4);
System.out.println("After swaping, ArrayList contains : " + arrayList);
     }
} 

Find Minimum element of Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class FindMinimumOfArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add(new Integer("327482"));
arrayList.add(new Integer("13408"));
arrayList.add(new Integer("802348"));
arrayList.add(new Integer("345308"));
arrayList.add(new Integer("509324"));
Object obj = Collections.min(arrayList);
System.out.println("Minimum Element of Java ArrayList is : " + obj);
   }
}

Get Enumeration over Java Vector

import java.util.Enumeration;
import java.util.Vector;
import java.util.Collections;
public class GetEnumerationOverVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
Enumeration e = Collections.enumeration(v);
System.out.println("Enumerating through Java Vector");
while(e.hasMoreElements())
System.out.println(e.nextElement());
   }
}

Get Enumeration over Java Hash Set

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Collections;
public class GetEnumerationOverHashSetExample
{
public static void main(String[] args)
{
HashSet hashSet = new HashSet();
hashSet.add("A");
hashSet.add("B");
hashSet.add("D");
hashSet.add("E");
hashSet.add("F");
Enumeration e = Collections.enumeration(hashSet);
System.out.println("Enumerating through Java HashSet");
while(e.hasMoreElements())
System.out.println(e.nextElement());
   }
}

Get Enumeration over Java Array List

import java.util.Enumeration;
import java.util.ArrayList;
import java.util.Collections; 
public class GetEnumerationOverArrayListExample
{
 public static void main(String[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("D");
arrayList.add("E");
arrayList.add("F");
Enumeration e = Collections.enumeration(arrayList);
System.out.println("Enumerating through Java ArrayList");
while(e.hasMoreElements())
System.out.println(e.nextElement());
    }
}

Enumerate through a Vector using Java Enumeration

import java.util.Vector;
import java.util.Enumeration; 
public class EnumerateThroughVectorExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("One");
v.add("Two");
v.add("Three");
v.add("Four");
Enumeration e = v.elements();
System.out.println("Elements of the Vector are : ");
while(e.hasMoreElements())
System.out.println(e.nextElement());
   }
}

Create Java Array List From Enumeration

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;   
public class CreateArrayListFromEnumerationExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
System.out.println("Vector contains : " + v);  Enumeration e = v.elements();
ArrayList aList = Collections.list(e);
System.out.println("Arraylist contains : " + aList);
    }
}

No comments:

Post a Comment