Would suggest you that for Java Program examples see this
http://www.idevelopment.info/data/Programming/java/PROGRAMMING_Java_Programming.shtml
************************************
First Program :
************************************
class Name
{
private String name;
public Name()
{
name=new String("Jumba");
}
public Name(String N)
{
name=new String(N);
}
public String getName()
{
return name;
}
public void setName(String N)
{
name=N;
}
public String toString()
{
return name;
}
}
class MainClass
{
public static void main(String args[])
{
Name N=new Name();
System.out.println(N.toString());
N.setName("Jumba1");
System.out.println(N.getName());
}
}
***************************************************
IInd Program :
**************************************************
public class ArrayListExample {
/**
* Provides an example of how to work with the ArrayList container.
*/
public void doArrayListExample() {
final int MAX = 10;
int counter = 0;
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Create/Store objects in an ArrayList container. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
List listA = new ArrayList();
List listB = new ArrayList();
for (int i = 0; i < MAX; i++) {
System.out.println(" - Storing Integer(" + i + ")");
listA.add(new Integer(i));
}
System.out.println(" - Storing String(Alex)");
listA.add("Alex");
System.out.println(" - Storing String(Melody)");
listA.add("Melody");
System.out.println(" - Storing String(Jeff)");
listA.add("Jeff");
System.out.println(" - Storing String(Alex)");
listA.add("Alex");
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an ArrayList container using an Iterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
Iterator i = listA.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an ArrayList container using a ListIterator. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
counter = 0;
ListIterator li = listA.listIterator();
while (li.hasNext()) {
System.out.println("Element [" + counter + "] = " + li.next());
System.out.println(" - hasPrevious = " + li.hasPrevious());
System.out.println(" - hasNext = " + li.hasNext());
System.out.println(" - previousIndex = " + li.previousIndex());
System.out.println(" - nextIndex = " + li.nextIndex());
System.out.println();
counter++;
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Retrieve objects in an ArrayList container using index. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
for (int j=0; j < listA.size(); j++) {
System.out.println("[" + j + "] - " + listA.get(j));
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Search for a particular Object and return its index location. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
int locationIndex = listA.indexOf("Jeff");
System.out.println("Index location of the String \"Jeff\" is: " + locationIndex);
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Search for an object and return the first and last (highest) index. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("First occurance search for String \"Alex\". Index = " + listA.indexOf("Alex"));
System.out.println("Last Index search for String \"Alex\". Index = " + listA.lastIndexOf("Alex"));
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Extract a sublist from the main list, then print the new List. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
List listSub = listA.subList(10, listA.size());
System.out.println("New Sub-List from index 10 to " + listA.size() + ": " + listSub);
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Sort the Sub-List created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Original List : " + listSub);
Collections.sort(listSub);
System.out.println("New Sorted List : " + listSub);
System.out.println();
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Reverse the Sub-List created above. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Original List : " + listSub);
Collections.reverse(listSub);
System.out.println("New Reversed List : " + listSub);
System.out.println();
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Check to see if the Lists are empty. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("Is List A empty? " + listA.isEmpty());
System.out.println("Is List B empty? " + listB.isEmpty());
System.out.println("Is Sub-List empty? " + listSub.isEmpty());
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Clone the initial List. |");
System.out.println("| NOTE: The contents of the List are object references, so both |");
System.out.println("| of the List's contain the same exact object reference's. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println("Sub-List (before) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
listB = new ArrayList(listA);
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println("Sub-List (after) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Shuffle the elements around in some Random order for List A. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println("Sub-List (before) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
Collections.shuffle(listA, new Random());
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println("Sub-List (after) : " + listSub);
System.out.println();
System.out.println("Are List's A and B equal? " + listA.equals(listB));
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Convert a List to an Array. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
Object[] objArray = listA.toArray();
for (int j=0; j < objArray.length; j++) {
System.out.println("Array Element [" + j + "] = " + objArray[j]);
}
System.out.println();
System.out.println("+---------------------------------------------------------------------+");
System.out.println("| Remove (clear) Elements from List A. |");
System.out.println("+---------------------------------------------------------------------+");
System.out.println();
System.out.println("List A (before) : " + listA);
System.out.println("List B (before) : " + listB);
System.out.println();
listA.clear();
System.out.println("List A (after) : " + listA);
System.out.println("List B (after) : " + listB);
System.out.println();
}
/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
ArrayListExample listExample = new ArrayListExample();
listExample.doArrayListExample();
}
}