我应该如何在泛型类之外声明我的泛型方法? (爪哇)

时间:2021-02-23 06:19:22

标签: java generics linked-list singly-linked-list

所以我在一个名为 SortLinked 的通用类中编写了一个名为 MergeList 的通用方法。 MergeList 方法使用递归来浏览两个已排序的链表,并返回一个包含已排序元素的单个链表。

问题是,一旦我将链表初始化为整数类型,我似乎就无法声明 MergeList 方法。有人可以就此提出建议吗?提前致谢!

这是代码:


public class SortLinked<T>{
    
    
    /*
     *  Method 2: using recursion
     *  
     *  We have two singly linked lists that are SORTED. So, we can start by comparing the heads of both.
     *  Whichever head has a smaller value, will be added to a new linked list. 
     *  The other head, will be called to MergeList with themselves and the next node in the other list
     *  And so on
     *  
     */
    
    public String MergeList(LinkedListG<T>.Node<T> node1, LinkedListG<T>.Node<T> node2) {
        
        
        LinkedListG<T> finalList = new LinkedListG<T>();  // we will add to this
        
        
        // if head 1 is null and not head 2, add head 2 to list
        
        if(node1 == null && node2!= null) {
            finalList.add(node2.data);
        }
        
        // if head 2 is null and not head 1, add head 1 to list
        
        else if (node2 == null && node1 != null) {
            finalList.add(node1.data);
        }
        
        // if neither is null, compare them
        
        else if (node1.compareTo(node2) == -1) {          // if node1 is smaller, we copy node1
            finalList.add(node1.data);
            node1 = node1.next;
            MergeList(node1, node2);
        }
        
        else if (node1.compareTo(node2) == 1) {          // if node2 is smaller, we copy node2
            finalList.add(node2.data);
            MergeList(node2.next, node1);
        }
            
        else {                                          // if both nodes are equal, we copy one of them (whichever)
            finalList.add(node1.data);
        }
        
        // print out?
        //System.out.println("Merged list is: " + finalList.printOut());
        
        return ("Merged list is: " + finalList.printOut());
    }
    
    
    
    
    
    // RUN THESE
    
    public static void main(String[] args) {

        LinkedListG<Integer> listOne = new LinkedListG<>();
        
        listOne.add(1);
        listOne.add(6);
        listOne.add(10);
        listOne.add(22);
        
        LinkedListG<Integer> listTwo = new LinkedListG<>();
        
        listTwo.add(2);
        listTwo.add(4);
        listTwo.add(6);
        listTwo.add(24);
        listTwo.add(30);
        
        // testing: System.out.println("Get Class() prints: " + listOne.head.data.getClass());
        
        System.out.println("List 1 is: ");
        listOne.printOut();
        
        System.out.println("\nList 2 is: ");
        listTwo.printOut();
        
        // merge them and print it
        // answer should be 1, 2, 4, 6, 10, 22, 24, 30
        
        String MergeList = SortLinked.<Integer>MergeList(listOne.head, listTwo.head);
        
        MergeList(listOne.head, listTwo.head);
        
        LinkedListG<Integer> stuff = MergeList(listOne.head, listTwo.head);
        
        System.out.println("\nThe merged list is: " + printOut(stuff) );
        
    }




}

0 个答案:

没有答案