Khurram's World - JAVA Abstract Window Toolkit
 
 
..Islam and Religion
 .Computer & IT
  Web Designing TIPS
..HTML & DHTML
..Active Server Pages
..JAVA & JAVA Applets
..JAVA & VB Script
..CGI & Perl
..Electronic Commerce
..Web Sites & Emails
 
Pakistan News Service
Bay Banner
 
 
 
java.util.package  

Objective:

        Learn about:

  • What are Arrays, Vectors and Hashtables (java.util Package)
  • How they created and managed

 

Concept:

Array

An array is a consecutive group of memory locations with the same name and type. To access a particular in an array we give the name of array and the position number of that particular element in the array. This position number is called the subscript. The first element of an Array has a subscript of 0 and the next has 1. So an array of 10 elements has subscript that varies from 0-9.

 

Declaration and syntax:

As an array is a group of memory locations and occupies space in memory therefore data type must be assigned to the array.

 

int a[];

a = new int [10];

In the above example we first declare the array and then allocate a particular length of memory to it. Another way of declaring an array is:

int x[] = {1,2,3,4,5};

 

if we know the number of data elements of an array we can use this method.

 

Example:

public class MyArray {

        public static void method (String args[]) {

                int x [] = new int [10];

                for (int i=0; i < 10; i++) {

                        x[i] = i;

                System.out.println(“Array elements are “ + x[i]);

                }

        }

}

 

Multi Dimensional Arrays
An array that uses two subscripts to specify any element in an array is called a Multi-Dimensional Array or a Multi-Subscripted Array. These arrays are just like graphs as in graphs each point has two coordinates. Similarly in a multi-subscripted array each element is specified by two subscript.

Duration and Syntax:

int Multi[] = new int[4][5];

Now this array is termed as a 4 by 5 array as it has 4 rows and 5 columns.

 

        int Multi[] = new int [4][];        //correct statement

        int Multi[] = new int [][5];        //incorrect statement

 

Another way of declaring this kind of array is,

        int x [] [] = { { 1,3,5,7}, {2,4,6,8} }

 

A multi-dimensional array in which each row has a different number of columns can be allocated as:

        int y [] [] = new int [3] [];

        y[0] = new int [2];

        y[1] = new int [3];

        y[2] = new int [4];

 

Example:

public class MyArray2 {

        public static void main (String args[]) {

                int multi [] [] = new int [3] [4];

                        for (int i = 0; i < multi.length; i++) {

                                for (int j=0; j < multi[i].length; j++) {

                                        multi[i][j] = j * 2;

                                        System.out.println(“Array is “ + multi[i][j]);

                                }

                        }

        }

}

 

Searching Arrays

Searching is a process of locating a particular element in an array. We get the search key from the user and search it into the whole array. Where the value matches the required search key, prompt message is generated. This kind of searching is used when the data in the array is not in a particular order.

 

Example

public class ArraySearch {

        public static void main (String args[]) {

                int array [] = new int[20];

                int key, i;

                boolean found = false;

                if (args.length i = 1) {

                        System.out.println(“Please enter some value”);

                        return

                }

                for (int j = 0; j < array.length; j++) {

                        array[j] = 3 * j;          //Data entered into the array.

                        System.out.println(“Array[“ + j + “] is: “ + array[j];

                }

                key = integer.parseInt(args[0]);

                for (i = 0; i < array.length; i++) {

                        if (key == array[i]) {

                                found = true;

                                break;

                        }

                }

                if (!found)

                        System.out.println(“Search key not found”);

                else

                        System.out.println(“Search key found at index “ = i);

        }

}     

 

Sorting Arrays

Sorting is a process of arranging the elements of arrays in either ascending or descending order. We can do it by declaring a small temp variable.

 

Example:

public class SortArray {

        public static void main (String args[]) {

                int array [] = {10, 5, 7, 0, 25, 38, 6, 1, 59, 4};

                for (int k = 0; k < array.length; k++)

                System.out.println(“Array[“ + k + “] is: + “array[k]);

                for (int p = 0; p < array.length; p++)

                for (int i = 0; i < array.length-1; i++) {

                        if (array[i] > array[i + 1] {

                                int hold;

                                hold = array[i];

                                array[i] = array[i + 1];

                                array[i + 1] = hold;

                        }

                }

        for (int j = 0; j < array.length; j++) {

                System.out.println(“Sorted array[“ + j + “] is: “ array[j]);

        }

}

Vectors:

Vectors implement dynamic arrays. They are similar to arrays that they can store values and reference them by an index but a vector objects can grow and shrink as needed. An element can be inserted into any location of a vector.

Another advantage of Vector arrays is that it is not restricted to store a particular type. A vector object manages a list of references. A reference of any type of object can be added to a vector. A vector class provides many methods to manage the list. Constructors are:

        Vector (int size);

        Vector (int size, int inc);

 

Important methods are:

        addElement (Object element)

        removeElement (Object element)

        firstElement ()

        lastElement ()

        size ()

 

Declaration and Syntax:

        Vector v = new Vector ();

        Vector v = new Vector (3, 2);    //3- (size) and 2- (increment)

 

Example:

import java.util.*;

 

class Dynamic {

        public static void main (String args[]) {

                Vector v = new Vector ();

 

                //adding new elements;

                v.addElement (“One”);

                v.addElement (“Two”);

                v.addElement (“Three”);

 

                //displaying the vector Elements

                System.out.println(“Vector Contains: “ + v);

 

                //displaying size of vector

                System.out.println(“The size of vector: “ v.size());

 

                //displaying first and last elements of vector

                System.out.println(“First Element: “ + v.firstElement());

                System.out.println(“Last Element: “ + v.lastElement());

                //getting a specific Element of a vector

                System.out.println(“Element three is at: “ v.indexOf(“Three”));

 

                //removal of Vector Element giving Index no.

                System.out.println(“Removing element at position 2”);

                v.removeElementAt(2);

                System.out.println(“Vector now contains” + v);

 

                //removal of vector element by giving Element name

                System.out.println(“Removing element one”);

                v.removeElement(“one”);

 

                //displaying new Vector status

                System.out.println(“Vector now contains “ + v);

                System.out.println(“New Vector size is: “ v.size());

        }

}

 

Hashing / Hashtable:

Hashing is technique for storing items so that they can be efficiently searched within a table. Hashtable stores the key/value pair in the hashtable. While using a hashtable, we specify an object that is used as a key, and the value that is linked to that key. The resulting hash code is used as the search index within the Hashtable. The constructors are:

        Hashtable ()

        Hashtable (int size)

 

The first is the default constructor. The second version creates a hashtable that has an initial size specified by size. Important methods are:

 

        boolean contains (Object value)

        boolean containsKey (Ojbect key)

        boolean isEmpty ()

        Object put (Object key, Object value)

        Object get (Object key)

        Object remove (Object key)

        int size ()

 

Declaration and Syntax:

        Hashtable h = new Hashtable ();

 

Example

import java.util.*;

 

class Hashme {

        public static void main (String arg[]) {

                Hashtable h = new Hashtable ();

 

                //put new elements in a hash table

                h.put(“one”, new Integer(32510) );

                h.put(“Two”, new Integer(52510) );

                h.put(“Three”, new Integer(12013) );

                System.out.println(“Hash table contains: “ + h);

 

                // Check if the hash table is empty

                System.out.println(“Hash table is empty? “ + h.isEmpty());

 

                // getting value from a hash table against a key

                System.out.println(“Two contains Value: “ + h.get(“Two”));

 

                //finding a value

System.out.println(“The value 32510 exists? “ + h.contains (new Integer(32510)));

 

                //finding a key

System.out.println(“The key three exists? “ + h.containsKey (“Three));

 

//removing an object against a key

System.out.println(“Removing three: “ + h.remove (“Three”));

 

//finding the latest status of hash table

System.out.println(“Hash table now contains: “ + h);

System.out.println(“Size is: “ + h.size());

        }

}