Uploaded by diyes81332

List Interface

advertisement
Advance Java Practical
Date – 15/09/2023
Objective: WAP to implement list Interface with generic class and also implement all the
method of list interface.
The Code:package AbhijeetMCA;
import java.util.ArrayList;
import java.util.List;
public class GenericList2<T> {
private List<T> list = new ArrayList<>();
// add element method
public void addElement(T element) {
list.add(element);
}
// remove element method
public void removeElement(T element) {
list.remove(element);
}
//checking if list has the asked element
public boolean contains(T element) {
return list.contains(element);
}
// is empty method (check if list is empty or not)
public boolean isEmpty() {
return list.isEmpty();
}
// list size
public int size() {
return list.size();
}
// method to print the list
@Override
public String toString() {
return list.toString();
}
public static void main(String[] args) {
System.out.println("Name: Abhijeet Gaikwad"); // Updated the name
System.out.println("Div: A");
System.out.println("Roll_no.: C23033\n"); // Updated the rollnumber
// Creating instances of GenericList for different data types
GenericList<Integer> integerList = new GenericList<>();
GenericList<Double> doubleList = new GenericList<>();
GenericList<String> stringList = new GenericList<>();
//Adding elements to the lists
integerList.addElement(98);
integerList.addElement(25);
integerList.addElement(55);
integerList.addElement(11);
integerList.addElement(27);
doubleList.addElement(2.3);
doubleList.addElement(5.1);
doubleList.addElement(11.6);
doubleList.addElement(0.0);
stringList.addElement("Abhijeet");
stringList.addElement("Mca");
stringList.addElement("Java");
stringList.addElement("Practical2");
Name: Abhijeet Gaikwad
Roll No: C23033 (A)
Advance Java Practical
Date – 15/09/2023
//printing lists
System.out.println("integerList: " + integerList);
System.out.println("doubleList: " + doubleList);
System.out.println("stringList: " + stringList);
// use of is empty to check if list is empty or not
System.out.println("Is integerList empty? : " +
integerList.isEmpty());
System.out.println("Is doubleList empty? : " +
doubleList.isEmpty());
System.out.println("Is stringList empty? : " +
stringList.isEmpty());
// asking size of the lists
System.out.println("Size of integerList: " + integerList.size());
System.out.println("Size of doubleList: " + doubleList.size());
System.out.println("Size of stringList: " + stringList.size());
// elements are present in the lists or not asking
System.out.println("Does integerList contain 98? " +
(integerList.contains(98) ? "Yes" : "No"));
System.out.println("Does doubleList contain 11.6? " +
(doubleList.contains(11.6) ? "Yes" : "No"));
System.out.println("Does stringList contain 'Abhijeet'? " +
(stringList.contains("Abhijeet") ? "Yes" : "No"));
// Removing an element from stringList
stringList.removeElement("Mca");
System.out.println("stringList (After removing 'Mca'): " +
stringList);
}
}
Name: Abhijeet Gaikwad
Roll No: C23033 (A)
Advance Java Practical
Date – 15/09/2023
Outputs:
Normal Output
Output when list is empty
Name: Abhijeet Gaikwad
Roll No: C23033 (A)
Download