Monday, January 16, 2012

Converting an ArrayList into an array.


//Obtaining an Array from an ArrayList

import java.util.*;
class ArrayListToArray
{
public static void main(String args[])
{
// Create an array list
ArrayList<Integer> al = new ArrayList<Integer>();

// Add elements to the array list
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
System.out.println("Contents of al: " + al);

// get array
Object ia[] = al.toArray();
int sum = 0;

//Display using array
System.out.println("Display using array");
for(int i=0;i<ia.length;i++)
{
System.out.println(ia[i]);
}

// sum the array
for(int i=0; i<ia.length; i++)
sum += ((Integer) ia[i]).intValue();
System.out.println("Sum is: " + sum);
}
}

Output:

Contents of al: [1, 2, 3, 4]
Display using array
1
2
3
4
Sum is: 10


Note:
Reasons to convert ArrayList to Array:
  • To obtain faster processing times for certain operations.
  • To pass an array to a method that is not overloaded to accept a collection.
  • To integrate collection-based code with legacy code that does not understand collections.

Array List


import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
// create an array list
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " +
al.size());

// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");

System.out.println("Size of al after additions: " +
al.size());

// display the array list
System.out.println("Contents of al: " + al);

// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);

//Iterator to display elements
System.out.print("Displaying contents of al using Iterator: ");
Iterator itr = al.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

//For each, alternative to iterator
System.out.print("Displaying contents of al using for each: ");
for(String s:al)
{
System.out.print(s + " ");
}
System.out.println();

// modify objects being iterated
ListIterator litr = al.listIterator();
while(litr.hasNext())
{
Object element = litr.next();
litr.set(element + "+");
}

System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious())
{
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();


}
}


Output:

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
Displaying contents of al using Iterator: C A2 E B D
Displaying contents of al using for each: C A2 E B D
Modified contents of al: C+ A2+ E+ B+ D+
Modified list backwards: D+ B+ E+ A2+ C+


Note:
  • ArrayList implements only List interface.

Sunday, January 15, 2012

Sorting and Searching using in build Arrays methods


import java.util.Arrays;
class Sorting
{
static void display(int array[])  //Static method for Displaying
{
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
System.out.println("");
}

public static void main(String args[])
{
int array[]=new int[10];

for(int i=0;i<10;i++)
array[i]=-3*i; //assigning some values in single dimensional array

System.out.println("Original contents:");  //Displaying original Contents
display(array);

Arrays.sort(array); //calling sort method to sort the contents
System.out.println("Sorted:");
display(array);

Arrays.fill(array,2,6,-1); //replacing the 0th position to 5th position with "-1"
System.out.println("After fil():");
display(array);

Arrays.sort(array); //sorting after inserting -1
System.out.println("After sorting:");
display(array);

System.out.println("The value -9 is at location"); //Searching the "-9" in the array by calling binary Search method
int index=Arrays.binarySearch(array,-9);
System.out.println(index);

}
}

Arrays


1. Two Dimentional Array Example

class ArrayDemo
{
public static void main(String args[])
{
int[]num={1,2,3,4,5}; //Single Dimensional Array
System.out.println("Array Length:" + num.length);

int k=0;
int [][] twoD=new int[4][5]; //Two Dimensional array (even columns)
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
twoD[i][j]=k;    //Storing 1 to 19 in two dimentional array
k++;
}

}

for(int i=0;i<4;i++)      //Displaying the elements from two dimentional array
{
for(int j=0;j<5;j++)
{
System.out.print(twoD[i][j]+"   ");
}
System.out.println();

}

}
}

Output:

Array Length:5
0   1   2   3   4
5   6   7   8   9
10   11   12   13   14
15   16   17   18   19

2. Two Dimensional (Uneven column) Array Example

public class Uneven
{
public static void main(String args[])
{
int [][] twoD=new int[4][];      //Two dimensional with Uneven columns array declaration
int k=0;
twoD[0]=new int[1];
twoD[1]=new int[2];
twoD[2]=new int[3];
twoD[3]=new int[4];
for(int i=0;i<4;i++)
{
for(int j=0;j<i+1;j++)
{
twoD[i][j]=k;
k++;
}
}

for(int i=0;i<4;i++)
{
for(int j=0;j<i+1;j++)
{
System.out.print(twoD[i][j]+" ");
}
System.out.println();
}
}
}

Output:

0
1 2
3 4 5
6 7 8 9


Sunday, January 1, 2012

Reflect Concept (Display all methods in a class including inherited class methods)


import java.lang.reflect.Method;
public class Reflect
{
public static void main(String args[])
{
CarB ob1=new CarB();
    Class c1=ob1.getClass();
    Method[] meths=c1.getMethods();
    for(Method m:meths)
    {
      System.out.println(m.getName());
    }
}
}

Note:  public class CarB extends Automobile implements IVehicle,IBox

output:

It displays all the methods in CarB, Automobile class

Palindrome Code


import java.util.*;
class palin12
{
public static void main(String args[])
{
String s1;
String s2="";
System.out.println("Enter the String");
Scanner scan=new Scanner(System.in);
s1=scan.next();

int l=s1.length();
char c[]=new char[l];
for(int i=l-1;i>=0;i--)
{
c[i]=s1.charAt(i);
s2=s2+c[i];
}
System.out.println(s2);
if(s1.equals(s2))
{
System.out.println("palindrome");
}
else
{
System.out.println("not palindrome");
}
}
}

output:
Enter the String

malayalam
malayalam
palindrome


Enter the String
bharath
htarahb
not palindrome




Passing Variable Length Arguments using ...

  • Ambiguity may occur in this method.
PassMulti.java:

class PassMulti
{

/*public void PassTest(int ... v)
{
System.out.println("No of args:" + v.length);
for(int i:v)
System.out.println(i);
}
*/

public void PassTest(double ... v)
{
System.out.println("No of args:" + v.length);
for(double i:v)
System.out.println(i);
}

public void PassTest(int a,boolean ... v)
{
System.out.println("No of args:" + v.length);
System.out.println(a);
for(boolean i:v)
System.out.println(i);
}

public void PassTest(String ... v)
{
System.out.println("No of args:" + v.length);
for(String i:v)
System.out.println(i);
}

}

PassMultiDemo.java


public class PassMultiDemo
{
public static void main(String args[])
{
//int a[]={10,20,30};
//int b[]={5,6};
//int c[]={};
PassMulti ob1=new PassMulti();
PassMulti ob2=new PassMulti();
PassMulti ob3=new PassMulti();

ob1.PassTest(1,2,3,4,5);
ob2.PassTest(10.20,50.5,70.00);
ob3.PassTest(10,true,false);
ob3.PassTest("Bharath","Hussain");

}
}

output:
javac PassMulti.java
javac PassMultiDemo.java
java PassMulti

No of args:5
1.0
2.0
3.0
4.0
5.0
No of args:3
10.2
50.5
70.0
No of args:2
10
true
false
No of args:2
Bharath
Hussain