Thursday, 15 October 2015

Vector example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Vector<String> obj = new Vector<String>();
obj.add("India");
obj.add("Digital");
obj.add("Message");
obj.add("Super");
Iterator it = obj.iterator();
while(it.hasNext()){
System.out.println("the values are " +it.next());

}

}
}

output:-
the values are India
the values are Digital
the values are Message
the values are Super

LinkedList example in java

import java.util.*;


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<Integer> obj = new LinkedList<Integer>();
obj.add(10);
obj.add(12);
obj.add(13);
obj.add(23);
obj.add(13);
Iterator it = obj.iterator();
while(it.hasNext()){
System.out.println("The value is  " +it.next());
}
obj.remove(2);
System.out.println("The value is  " +obj);
int l = obj.size();
System.out.println("the value of l is " +l);
}
}


output:-
The value is  10
The value is  12
The value is  13
The value is  23
The value is  13
The value is  [10, 12, 23, 13]
the value of l is 4

LinkedHashSet example in java

import java.util.*;


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedHashSet<String> hs = new LinkedHashSet<String>();
hs.add("Digital");
hs.add("India");
System.out.println("The value of hs is " +hs);
for(String s : hs){
System.out.println("the value of s is " +s);
}
Iterator<String> it = hs.iterator();
while(it.hasNext()){
System.out.println("the value through iterator is " +(String)it.next());
}
}
}


output:-
The value of hs is [Digital, India]
the value of s is Digital
the value of s is India
the value through iterator is Digital
the value through iterator is India

Hashset example in java (using for-each loop)

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();
hs.add("Digital");
hs.add("India");
hs.add("Hello");
hs.add("World");
for(String st: hs){
System.out.println("The string value is " +st);
}

}

}


output:-
The string value is Hello
The string value is Digital
The string value is World
The string value is India

HashSet example in java

import java.util.*;
public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<String> hs = new HashSet<String>();
hs.add("India");
hs.add("Amrica");
hs.add("Singapore");
hs.add("Nepal");
System.out.println("The value of HashSet is " +hs);
Iterator<String> it = hs.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println("the value is " +s);
}
}

}

output:-
The value of HashSet is [Amrica, Singapore, Nepal, India]
the value is Amrica
the value is Singapore
the value is Nepal
the value is India

Wednesday, 14 October 2015

Array example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Employee array[] = new Employee[5];
for(int i=0;i<5;i++){
array[i]=new Employee(i,"Sai"+i);
System.out.println("The array values are " +array[i].age +"\t" +array[i].name );
}
}

}



public class Employee {
int age;
String name;
Employee(int i,String s){
age=i;
name=s;
}
}


output:-
The array values are 0 Sai0
The array values are 1 Sai1
The array values are 2 Sai2
The array values are 3 Sai3
The array values are 4 Sai4

Wrapper Class examples in java

import java.io.*;

public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Byte obj = new Byte((byte) 10);
Byte obj1 = new Byte("10");
Character obj2= new Character('A');
byte j=obj1.byteValue();
byte i=obj.byteValue();
int k = obj.compareTo(obj1);
System.out.println("The value of K is " +k);
System.out.println("The value of i is " +i);
System.out.println("The value of j is " +j);
char ch = obj2.charValue();
System.out.println("The value of char is " +ch);
Character obj3 = Character.valueOf(ch);
System.out.println("The value of char is " +obj3);


}
}


output:-
The value of K is 0
The value of i is 10
The value of j is 10
The value of char is A
The value of char is A