0% found this document useful (0 votes)
45 views4 pages

ASS4

The document discusses different examples of using generic methods and classes in Java. It includes examples of creating a generic queue class, sorting and displaying generic arrays, swapping elements in generic arrays, and using generic collection classes like ArrayList, LinkedList, and HashSet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

ASS4

The document discusses different examples of using generic methods and classes in Java. It includes examples of creating a generic queue class, sorting and displaying generic arrays, swapping elements in generic arrays, and using generic collection classes like ArrayList, LinkedList, and HashSet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

12202040501019 Dharmik Rabadiya

Assignment 4
1] Generic Queue Class

import java.util.LinkedList;

class GenericQueue<T> {
private LinkedList<T> queue;

public GenericQueue() {
queue = new LinkedList<>();
}

public void enqueue(T element) {


queue.addLast(element);
}

public T dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return queue.removeFirst();
}

public boolean isEmpty() {


return queue.isEmpty();
}
}

public class Main {


public static void main(String[] args) {
GenericQueue<Integer> integerQueue = new GenericQueue<>();
integerQueue.enqueue(10);
integerQueue.enqueue(20);
integerQueue.enqueue(30);

while (!integerQueue.isEmpty()) {
System.out.println("Dequeued: " + integerQueue.dequeue());
}

GenericQueue<String> stringQueue = new GenericQueue<>();


stringQueue.enqueue("One");
stringQueue.enqueue("Two");
stringQueue.enqueue("Three");

while (!stringQueue.isEmpty()) {
System.out.println("Dequeued: " + stringQueue.dequeue());
}
}
}

2] Generic Sorting Method

import java.util.Arrays;

public class Main {


public static <T extends Comparable<T>> void sortArray(T[] array) {
Arrays.sort(array);
12202040501019 Dharmik Rabadiya

public static void main(String[] args) {


Integer[] intArray = {4, 2, 8, 1, 6};
sortArray(intArray);
System.out.println("Sorted Integer Array: " +
Arrays.toString(intArray));

Double[] doubleArray = {3.5, 1.2, 7.8, 2.3, 5.6};


sortArray(doubleArray);
System.out.println("Sorted Double Array: " +
Arrays.toString(doubleArray));
}
}
3] Generic Method to Display Array Elements

public class Main {


public static <T> void displayArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}

public static void main(String[] args) {


Integer[] intArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
Character[] charArray = {'a', 'b', 'c', 'd', 'e'};

System.out.println("Integer Array:");
displayArray(intArray);

System.out.println("Double Array:");
displayArray(doubleArray);

System.out.println("Character Array:");
displayArray(charArray);
}
}
4] Generic Swap Function

public class Main {


public static <T> void swap(T[] array, int i, int j) {
if (i < 0 || i >= array.length || j < 0 || j >= array.length) {
throw new IllegalArgumentException("Invalid indices");
}

T temp = array[i];
array[i] = array[j];
array[j] = temp;
}

public static void main(String[] args) {


Integer[] intArray = {1, 2, 3, 4, 5};
System.out.println("Original Integer Array: " +
Arrays.toString(intArray));
swap(intArray, 0, 4);
12202040501019 Dharmik Rabadiya

System.out.println("Swapped Integer Array: " +


Arrays.toString(intArray));

String[] stringArray = {"One", "Two", "Three", "Four", "Five"};


System.out.println("Original String Array: " +
Arrays.toString(stringArray));
swap(stringArray, 1, 3);
System.out.println("Swapped String Array: " +
Arrays.toString(stringArray));
}
}
5] Program Demonstrating ArrayList, LinkedList, HashSet

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;

public class Main {


public static void main(String[] args) {
// ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");

System.out.println("ArrayList:");
System.out.println(arrayList);

// LinkedList
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Red");
linkedList.add("Green");
linkedList.add("Blue");

System.out.println("\nLinkedList:");
System.out.println(linkedList);

// HashSet
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");

System.out.println("\nHashSet:");
System.out.println(hashSet);
}
}
6] Integer List Example

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Main {


public static void main(String[] args) {
// Integer List Example
List<Integer> integerList = new ArrayList<>();
integerList.add(1);
12202040501019 Dharmik Rabadiya

integerList.add(2);
integerList.add(3);
integerList.add(4);
integerList.add(5);

integerList.add(2, 10);
System.out.println("After adding element at index 2: " +
integerList);

int element = integerList.get(3);


System.out.println("Element at index 3: " + element);

integerList.set(4, 20);
System.out.println("After replacing element at index 4: " +
integerList);

System.out.println("Traverse List elements using next() and


hasNext() methods:");
ListIterator<Integer> iterator = integerList.listIterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println("\n");

System.out.println("Difference between next() and hasNext()


methods:");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println("\n");
}
}
7] Program to Sort ArrayList in Descending Order

import java.util.ArrayList;
import java.util.Collections;

public class Main {


public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(5);
arrayList.add(15);
arrayList.add(20);
arrayList.add(1);

System.out.println("Original ArrayList: " + arrayList);

Collections.sort(arrayList, Collections.reverseOrder());

System.out.println("Sorted ArrayList (Descending Order): " +


arrayList);
}
}

You might also like