Search Java Programs

Tuesday, January 26, 2010

How to Sort of Array integer and String

import java.util.Arrays;

/**
 *
 * @author javadb.com
 */
public class testing {

    /**
     * Example method for sorting an int array
     */
    public void sortIntArray() {

        int[] arrayToSort = new int[] {48, 5, 89, 80, 81, 23, 45, 16, 2};

        Arrays.sort(arrayToSort);

        for (int i = 0; i < arrayToSort.length; i++)
        {
            System.out.print(arrayToSort[i] + " ");
        }
        System.out.println("\n");
    }
    

    /**
     * Example method for sorting a String array
     */
    public void sortStringArray() {

        String[] arrayToSort = new String[] {"Oscar", "Charlie", "Ryan", "Adam", "David"};

        Arrays.sort(arrayToSort);

        for (int i = 0; i < arrayToSort.length; i++)
        {
            System.out.print(arrayToSort[i] + " ");
        
        }
        System.out.println("\n");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        testing main = new testing();
        main.sortIntArray();
        main.sortStringArray();
    }
}

How to use ArrayList in Java

// Demonstrate ArrayList.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
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("H");
al.add(2, "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(6);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}

Wednesday, January 20, 2010

Java String valueOf Example

String class valueOf method example.


String class valueOf method example:- This example demonstrates the working of valueOf method. this method returns String representations of all data types values

Syntax:-valueOf(boolean b)

Here is the code:-

 /**
* @(#) ValueOfString.java
* ValueOfString class demonstrates the working of valueOf() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/



public class ValueOfString {
public static void main(String args[]) {

// method converts any data type value in to String type
boolean result = true;
int in = 234;
char ch = 'u';
double dou = 34.86;
float flt = 54.889f;

System.out.println("String represented boolean value: "
+ String.valueOf(result)
+ "\nString represented integer value: " + String.valueOf(in)
+ "\nString represented char value: " + "'"
+ String.valueOf(ch) + "'"
+ "\nString represented double value: " + String.valueOf(dou)
+ "\nString represented float value: " + String.valueOf(flt));

}
}
Output of the program:-
String represented boolean value: true
String represented integer value: 234
String represented char value: 'u'
String represented double value: 34.86
String represented float value: 54.889

Website Design by Mayuri Multimedia