I am trying to implement a generic linked list. It works fine except for a small
ID: 3748165 • Letter: I
Question
I am trying to implement a generic linked list. It works fine except for a small detail. When I attempt to use the insert method it will only work properly if both linked list are of the same type (two linked list of String or two linked lists of Integer for example) However, if i try to insert a String linked list into an Integer linked list i get an error. I do not understand why this happens, I thought that my use of generic nodes and declaring my lists as GenericLinkedList<String> list1 = new GenericLinkedList<>(); and GenericLinkedList<String> list2 = new GenericLinkedList<>(); would make it such taht I would not be having the problem I am having.
A more detailed description of the method: receives a generic List (a Java List) and an index position as parameters, and copies each value of the passed list into the current list starting at the index position, provided the index position does not exceed the size. For example, if list has a,b,c and another list having 1,2,3 is inserted at position 2, the list becomes a,b,1,2,3,c
in my program if list 1 is 1 1 1 1 11 1 and i insert 3 3 3 it will work but if my list is 1 1 1 1 1 1 and i insert a b c i get an error.... why ?? What do I need to do to fix this? Ive included the class headers in my code below.
Thanks!
public class DriverMain {
public static void main (String [] args) {
GenericLinkedList<String> list1 = new GenericLinkedList<>();
GenericLinkedList<Integer> list2 = new GenericLinkedList<>();
list1.addToFront("a");
list1.addToFront("b");
list1.addToFront("c"); /// and so on.....
list2.addToFront(1);
list2.addToFront(2);
list2.addToFront(3);
list1.insert(list2, 2);
}
}
public class Node<AnyType> {
// data and pointer fields here
}
public class GenericLinkedList<AnyType> {
// all methods including addToFront(Node<AnyType n); and insert(List<AnyType> L, int i);
}
Explanation / Answer
I think you have misunderstood the concept of generics here. The use of generics does not mean that the data type can be anything at any time. If you create a generic class, it can create any type of objects extending Object class, but once you create an object of a generic class, it cannot be used to perform operations with another instance having a different datatype. For instance, consider the below code.
GenericLinkedList<String> list1 = new GenericLinkedList<>();
GenericLinkedList<Integer> list2 = new GenericLinkedList<>();
Here, the list1 object becomes a generic list of String objects, so it can handle only Strings. Similarly list2 becomes a generic list of integers. So it can handle only integers. Now let’s check the insert() method in GenericLinkedList. The parameters are a list of <AnyType> and an integer specifying index. Here, the value of AnyType is given during the creation of object, which is String for list1 and Integer for list2. So obviously, insert() method of list1 can only handle lists of String type and insert() method of list2 can only handle lists of Integer type; which is the reason why you cannot insert list2 into list1.
If you really need to insert a list to another, it really needs some work. First of all, you’ll need to modify the type of list argument in insert() method from AnyType to Object. So that it can accept lists of any type extending Object class (by default, all objects). Then you’ll need to check if the list in parameter is compatible with the type of the list which it is being added to, It requires a lot of logical checks, like you can insert an integer list to string, by converting each integer into String using String.valueOf() or using some other techniques, and adding to the original String list. But you cannot add a String list to Integer list as Strings cannot be converted to integers.
I hope you can understand everything now. If you need more help, just drop a comment. Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.