Data Structures Using Java Notes
Data Structures Using Java Notes
1. Primitive data
2. Abstract data
3. Storing data
4. Retrieving data
• Encapsulation • Multithreading
• 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:
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
}
}
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);
}
}
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);
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,
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
}
}
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
}
}
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();
}
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
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
In parameter type, you can not use primitives type like int, char, double, etc. Only class can
be referred as the template data.