Wednesday 30 October 2013

Populate list with pojo class dynamically.

package com.demo.swain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListBox {
    String name,adress,city;
    int rollno,age;
   
    public static void main(String args[]){
        System.out.println("Name"+"  "+"adress"+"  "+"city"+"  "+"rollno"+"  "+"age");
        System.out.println("********************************************************");
        ListBox obj=new ListBox();
        List<ListBox> obj1=new ArrayList<ListBox>();
       
        obj.setAdress("odisha");
        obj.setAge(23);
        obj.setCity("bbsr");
        obj.setName("sitansu");
        obj.setRollno(209);
        obj1.add(obj);
        obj=new ListBox();
        obj.setAdress("A");
        obj.setAge(21);
        obj.setCity("dfsre");
        obj.setName("ewrtfs");
        obj.setRollno(5345);
        obj1.add(obj);
       
        obj=new ListBox();
        obj.setAdress("erew");
        obj.setAge(2312);
        obj.setCity("rrewfwf");
        obj.setName("xxvzv");
        obj.setRollno(434);
        obj1.add(obj);
       
        for(ListBox data:obj1){
            System.out.println(data.getAdress()+ "   "+data.getAge()+"   "+data.getCity()+"   "+data.getRollno()+"   "+data.getName());
        }
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAdress() {
        return adress;
    }
    public void setAdress(String adress) {
        this.adress = adress;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public int getRollno() {
        return rollno;
    }
    public void setRollno(int rollno) {
        this.rollno = rollno;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }   
}

Saturday 20 April 2013

Using toString in java


//toString 
package Strings.swain;

public class sortString {
    static String arr[]={
        "Now","is","time","for","all","good","men","to","come","to","come","to","the","aid","of","their","country"
    };

    public sortString() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
for(int j=0;j<arr.length;j++){
    for(int i=j+1;i<arr.length;i++){
        if(arr[i].compareToIgnoreCase(arr[j])<0){
            String t=arr[j];
            arr[j]=arr[i];
            arr[i]=t;
        }
    }
    System.out.println(arr[j]);
}
    }

}

Using toString in java

//using to string in java
package Strings.swain;

public class sortString {
    static String arr[]={
        "Now","is","time","for","all","good","men","to","come","to","come","to","the","aid","of","their","country"
    };

    public sortString() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
for(int j=0;j<arr.length;j++){
    for(int i=j+1;i<arr.length;i++){
        if(arr[i].compareToIgnoreCase(arr[j])<0){
            String t=arr[j];
            arr[j]=arr[i];
            arr[i]=t;
        }
    }
    System.out.println(arr[j]);
}
    }

}