Friday, October 31, 2014

Java array List



Compare Two Java long Arrays

import java.util.Arrays;
public class CompareLongArraysExample
{
public static void main(String[] args)
{
long[] longArray1 = new long[]{213873,87210,320918};
long[] longArray2 = new long[]{213873,87210,320918};
boolean blnResult = Arrays.equals(longArray1,longArray2);  System.out.println("Are two long arrays equal ? : " + blnResult);
   }
}

Compare Two Java short Arrays

import java.util.Arrays;
 public class CompareShortArraysExample
{
public static void main(String[] args)
{
short[] shortArray1 = new short[]{107,93,58};
short[] shortArray2 = new short[]{107,93,58};
boolean blnResult = Arrays.equals(shortArray1,shortArray2);
System.out.println("Are two short arrays equal ? : " + blnResult);
   }
}

Create List from Java Object Array

import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
public class CreateListFromObjectArrayExample
{
public static void main(String[] args)
{
String[] strArray = new String[]{"Object","Array","Converted","To","List"};List list = Arrays.asList(strArray);
Iterator itr = list.iterator();
System.out.println("List created from an Array of type Object contains,");
while(itr.hasNext())
System.out.println(itr.next());
   }
}

Copy Elements of Array List to Java Vector

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class CopyElementsOfArrayListToVectorExample
{
 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");
Vector v = new Vector();
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
v.add("G");
v.add("H");
System.out.println("Before copy, Vector Contains : " + v);
Collections.copy(v,arrayList);     
System.out.println("After Copy, Vector Contains : " + v);  
}
}

Copy Elements of One Java ArrayList to Another Java Array List

import java.util.ArrayList;
import java.util.Collections;
public class CopyElementsOfArrayListToArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList1 = new ArrayList();
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");
ArrayList arrayList2 = new ArrayList();
arrayList2.add("One");
arrayList2.add("Two");
arrayList2.add("Three");
arrayList2.add("Four");
arrayList2.add("Five");
System.out.println("Before copy, Second ArrayList Contains : " + arrayList2);  Collections.copy(arrayList2,arrayList1);
System.out.println("After copy, Second ArrayList Contains : " + arrayList2);  
    }
}

Copy Elements of One Java Vector to Another Java Vector

import java.util.Collections;
import java.util.Vector; 
public class CopyElementsOfVectorToVectorExample
 {
public static void main(String[] args)
 {
Vector v1 = new Vector();
v1.add("1");
v1.add("2");
v1.add("3");
Vector v2 = new Vector();
v2.add("One");
v2.add("Two");
v2.add("Three");
v2.add("Four");
v2.add("Five");
System.out.println("Before copy, Second Vector Contains : " + v2);
Collections.copy(v2,v1);
System.out.println("After copy, Second Vector Contains : " + v2);  
   }
}

Copy Elements of Vector to Java Array List

import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class CopyElementsOfVectorToArrayListExample
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("1");
v.add("2");
v.add("3");
ArrayList arrayList = new ArrayList();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");
arrayList.add("Four");
arrayList.add("Five");
System.out.println("Before copy ArrayList Contains : " + arrayList);Collections.copy(arrayList,v);
System.out.println("After Copy ArrayList Contains : " + arrayList);  
    }
}

No comments:

Post a Comment