Data Structures Using Java Notes
Data Structures Using Java Notes
1. Primitive data
2. Abstract data
3. Storing data
4. Retrieving data
Java supports for programming
• Encapsulation • Multithreading
• Inheritance • AWT, Swing, JavaFX
• Package and interface • Networking
• Exception handling • JDBC
Java supports for data structures
• java.util
• class String
• java.io
Generic method:
A method that can refer to any data type is known as a generic method.
The syntax for declaring a generic method is as follows:
Example 2.1: A generic method for printing
class DemoClass {
// Defining a generic method to print any data type
void genericPrint (T t) {
System.out.println (t);
}
public static void main(String[] args) {
DemoClass aObj; // Creating an object of the class DemoClass
aObj.genericPrint(101); // Calling generic method with int argument
aObj.genericPrint("Joy with Java"); // Calling generic method with String
aObj.genericPrint(3.1412343); // Calling generic method with double
}
}
Example 2.2 : Static generic method
class StaticGenericMethodDemo{
// Defining a static generic method to print any data type
static<T> void genericPrint (T t){
//The following statement print which type parameter T this method is handling
System.out.println (t.getClass().getName() + ":"+t);
}
public static void main(String[] args){
genericPrint(101); // Calling generic method with integer argument
genericPrint("Joy with Java"); // Calling generic method with String argument
genericPrint(3.1412343); // Calling generic method with double argument
}
}
Note: Parameter(s) should be class type(s)
Example 2.3: Generic method for Integer swap operation
// Similar to double and string
class SwapTest1{
public static void swap(T x, T y){
T temp;
t=x;
x=y;
y=t;
}
public static void main(String args[]){
Integer x = new Integer(99);
Integer y = new Integer(66);
System.out.println("x = “ + x + " “ + "y = “ + y);
swap(x, y);
System.out.println("x = “ + x + " “ + "y = “ +y);
}
}
Example 2.6: Swap method with Object as parameters
class Person {
String name;
float marks;
Person(String name, float marks) {
this.name = name;
this.marks = marks
}
}
class SwapTest4{
public static void swap(Object x, Object y){
Object t;
t=x;
x=y;
y=t;
}
public static void main(String args[]){
Object p1 = new Person(“Sumit”, 99.9);
Double p2 = new Double(”Rahul”, 66.6);
System.out.println(“p1 = “ + p1 + " “ + "y = “ + p2);
swap(p1, p2);
System.out.println(“p1 = “ + p1 + " “ + "y = “ + p2);
}
}
Methods with Variable List of Parameters:
Declaration of “varargs” methods
1. Using an array
gMethod1(T[] t);
2. Using ellipsis ( three dots)
gMethod2(T ... t);
3. Using Object class
gMethod3(Object[] o);
Varargs Method:
The values which you want to pass to a method, store them in
an array and then pass the array to the method,
Example 2.7: varargs method using array
class VarargsMethodDemo1 {
static void varargsMethod1(int v[]) {
System.out.print("Number of args: " + v.length +" Elements: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
// Following arrays are created for test...
int x[] = { 1, 3, 5, 7 };
int y[] = { 2, 4};
int z[] = { };
varargsMethod1 (x); // Passed 4 values to the method
varargsMethod1 (y); // Passed 2 values to the method
varargsMethod1 (z); // Passed no argument to the method
}
}
Example 2.8: varargs method using Ellipsis
class VarargsMethodDemo2 {
//Defining a varargs method using ellipsis
static void varargsMethod2(int ...v) {
System.out.println("Number of arguments: " + v.length);
for (int i: v) // For each item i in array v
System.out.print(i + " ");
System.out.println();
}
public static void main(String args[]) {
// Calling the varargs method with variable arguments
varargsMethod2 (9); // One parameter
varargsMethod2 (1, -2, 3, -4); // Four parameters
varargsMethod2 (); // no parameter
}
}
Example 2.9: varargs method using Objects
class VarargsMethodDemo3 {
public static void varargsMethod3(Object ... obj) {
for(Object o : obj)
System.out.print(“ “ + o);
System.out.println( );
}
public static void main(String[] args) {
varargsMethod3( 1, “String”, 2.3, true); // Four arguments
varargsMethod3 (); // No arguments
varargsMethod3 (15, 25, 35, 45, 55); // Five arguments
}
}
Concept of Generic Class:
Why generic class?
Observations Different programs.
Data are different. Code duplications
Algorithms (i.e., logic) are same for all
methods.
Example 3.3 : Program to handle an array of Strings,Integer and Double
class SpecificArrayString {
// Declaring an array of double values
String c;
// Constructor to load the array
SpecificArrayDouble(String c[]) {
this.c = c;
}
// Method to print the array elements
void printString() {
for(String x : c)
System.out.println(x);
}
// Method to reverse the array elements
void reverseString() {
j = c.length;
for (int i=0; i<j; i++)
double temp;
temp = c[i];
c[i] = c[j];
c[j] = temp;
j--;
} // End of for-loop
} // end of method
} // end of class
class MainClassString {
//This class use the class SpecificArrayInt to manipulate data in it
SpecificArrayDouble b = {“A”, “B”, “C”, “D”, “E”};
c.printString();
c.reverseString();
c.printString();
}
Syntax for generic class definition
The syntax for declaring a generic class is as follows:
[<<Access] class <ClassName> <<Type1> [, <Type2>, …]> {
… body of the class
}
Here, is the full syntax for declaring a reference to a generic class and instance creation:
<className> <typeList> varName = new<className> <typeList> (<InputArray>);
Example 3.4 : Defining a generic class
public class GeneriClass <T> {
// Two elemnts of generic type T is defined below
private T x;
// Constructor
public GenericClass(T t) {
x = t;
}
// Print the T-type value for an object
public void printData() {
System.out.println (x);
}
} // This completes the definition of a simple generic class GeneriClass<T>
class GenericClassTest {
public static void main( String args[] ) {
// A data with the member as String
GenericClass<String> a = new GenericClass <String> ("Java");
a.printData();
// A data with the member as integer value
GenericClass<Integer> b = new GenericClass<Integer> (123);
b.printData();
// A data with the member as float value
GenericClass<Double> c = new GenericClass <Double> (3.142);
c.printData();
}
}
Example 3.5: Defining class to process arrays with any data type
class GenericArray<T> {
//Declaring an array, which should store any type T of data
T a[ ];
GenericArray(T x) { // Define a constructor
a = x;
}
T getData(int i) { // To return the element stored in the i-th place in the array
return a[i];
}
void static printData (T b) { // A generic method to print the elements in array b
for(int i = 0; i < b.length(); i ++)
System.out.print(b.getData(i) + " "); // Print the i-th element in b
System.out.println(); // Print a new line
}
void static reverseArray (T b) { // Generic method to reversed the order of elements in array b
int front = 0, rear = b.length-1;
T temp;
while( front < rear) {
temp = b[rear];
b[rear] = a[front];
a[front] = temp;
front++; rear--;
}
}
}
class GenericClassArrayDemo {
public void static main(String args a[]) {
//Creating an array of integer data
Integer x[]={10, 20, 30, 40, 50}; // It is an array of Integer numbers
// Store the data into generic array
GenericArray<Integer> aInt = new GenericArray<Integer>(x);
// Alternatively:
// GenericArray<Integer> aInt = new GenericArray<Integer>(new Integer x[ ] {10, 20, 30, 40, 50});
// Printing the data ...
printData(aInt); // Printing the array of integer objects
//Reverse ordering of data ...
reverseArray(aInt);
// Printing the data after reverse ordering ...
printData(aInt); // Printing the array of integer objects after reversing
//Creating an array of integer data
String y[]={“A”, “B”, “C”, “D”, “E”}; // It is an array of String objects
// Store the data into a generic array
GenericArray<String> bString= new GenericArray<String>(y);
// Printing the data ...
printData(bString); // Printing the array of string objects
//Reverse ordering of data ...
reverseArray(bString);
// Printing the data after reverse ordering ...
printData(bString); // Printing the array of string objects after reversing
//Creating an array of double data
Double z[]={1.2, 2.3, 3.4, 4.5, 5.6}; // It is an array of double values
// Store the data into a generic array
GenericArray<Double> cDouble= new GenericArray<Double>(z);
// Printing the data ...
printData(cDouble); // Printing the array of double values
//Reverse ordering of data ...
reverseArray(cDouble);
// Printing the data after reverse ordering ...
printData(cDouble); // Printing the array of double values after reversing
} // End of main method
} // End of GenericArrayClassDemo class
Example 3.6: Working with user defined class
//Define a class Student
class Student {
String name;
float marks;
Student(String name, float marks) {
this.name = name;
this.marks = marks;
}
class GenericClass<T> { // Use < > to specify class type
T obj; // An object of type T is declared
GenericClass(T obj) { // Constructor of the generic class
this.obj = obj;
}
public T getObject() { // A Method in the class
return this.obj;
}
}
class GenericClassTest { // Driver class to test generic class facility
public static void main (String[] args) {
GenericClass<Integer> iObj= new GenericClass<Integer>(15);
// A class with Integer type
System.out.println(iObj.getObject());
GenericClass<String> sObj= new GenericClass<String>("Java");
// Another class with String type
System.out.println(sObj.getObject());
GenericClass<Student> tObj = new GenericClass <Student>(”Debasis”, 99.9);
// Another class with Student type
System.out.println(tObj.getObject());
}
}
NOTE:- A generic method or any method in a generic class can be declared as static.
In parameter type, you can not use primitives type like int, char, double, etc. Only class can
be referred as the template data.
class GenericClass<T> { // Use < > to specify class type
T obj; // An object of type T is declared
GenericClass(T obj) { // Constructor of the generic class
this.obj = obj;
}}
class GenericClassDemo3 {
public void static main(String args a[]) {
GenericClass<Integer> a = new GenericClass<Integer>(123); // Okay
// GenericClass<int> a = new GenericClass<int>(234); // ERROR!
GenericClass<String> s = new GenericClass<String>("Joy with Java"); // Okay
// GenericClass<double> d = new GenericClass<double>(9.87); // ERROR!
GenericClass<Double> d = new GenericClass<Double>(1.23); // Okay
}}