Java中如何编写接收实现特定接口参数的方法?
ListInterface<T> and Fulfill the join() Contract in Its Concrete Classes Got it, let's walk through this step by step. You need two things here: a method that works with any ListInterface<T> implementation, and proper join() method implementations for both DoubleLinkedList and MyArrayList. Here's how to do it right in Java:
1. Writing a Method That Accepts ListInterface<T>
Thanks to polymorphism, you can use the interface directly as the parameter type—this lets your method work with any class that implements ListInterface<T>, including DoubleLinkedList and MyArrayList. Here's a practical example:
import your.package.path.ListInterface; public class ListHandler { // A reusable method that takes any ListInterface<T> instance public static <T> ListInterface<T> combineAndProcess(ListInterface<T> baseList, ListInterface<T> additionalList) { // Use the join method defined in the interface ListInterface<T> combinedList = baseList.join(additionalList); System.out.println("Combined list size: " + combinedList.size()); // Assume size() is added to the interface return combinedList; } }
You can call this with either concrete class seamlessly:
DoubleLinkedList<String> linkedList = new DoubleLinkedList<>(); MyArrayList<Integer> arrayList = new MyArrayList<>(); // Both calls are valid ListHandler.combineAndProcess(linkedList, new DoubleLinkedList<>()); ListHandler.combineAndProcess(arrayList, new MyArrayList<>());
2. Implementing join() in DoubleLinkedList
For a doubly linked list, the goal is to append every element from otherList to the end of the current list. Don't forget to handle the required IllegalArgumentException (like when otherList is null).
public class DoubleLinkedList<T> implements ListInterface<T> { // Assume your linked list internals are here: // private Node<T> head; // private Node<T> tail; // private int size; @Override public ListInterface<T> join(ListInterface<T> otherList) throws IllegalArgumentException { // Validate input first if (otherList == null) { throw new IllegalArgumentException("otherList cannot be null"); } // Skip if the other list is empty if (otherList.size() == 0) { return this; } // Iterate over the other list and add each element to our tail for (int i = 0; i < otherList.size(); i++) { T element = otherList.get(i); // Assume get() is in ListInterface addToTail(element); // Your existing helper method for adding to the end } // Return the modified original list as required return this; } // Example helper method for your linked list private void addToTail(T element) { Node<T> newNode = new Node<>(element); if (tail == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } size++; } // You'll need to implement size(), get(int), etc., in the class and add them to ListInterface }
3. Implementing join() in MyArrayList
For an array-backed list, you'll need to resize the underlying array if needed, then copy all elements from otherList into the end of your array.
import java.util.Arrays; public class MyArrayList<T> implements ListInterface<T> { // Assume your array list internals are here: // private T[] array; // private int size; // private static final int DEFAULT_CAPACITY = 10; @Override public ListInterface<T> join(ListInterface<T> otherList) throws IllegalArgumentException { if (otherList == null) { throw new IllegalArgumentException("otherList cannot be null"); } int otherSize = otherList.size(); if (otherSize == 0) { return this; } // Resize the array if there's not enough space for the combined elements ensureCapacity(size + otherSize); // Copy elements from otherList into our array for (int i = 0; i < otherSize; i++) { array[size++] = otherList.get(i); } return this; } // Helper method to resize the array when needed private void ensureCapacity(int minCapacity) { if (minCapacity > array.length) { int newCapacity = Math.max(minCapacity, array.length * 2); array = Arrays.copyOf(array, newCapacity); } } // Again, implement size(), get(int), etc., as required by the interface }
Quick Reminders
- Interface Completeness: The
join()method relies on methods likesize()andget(int)that aren't in your providedListInterface<T>—you'll need to add these to the interface so all implementing classes must provide them. - Polymorphism Benefits: By using
ListInterface<T>as your parameter type, your method will automatically work with any new classes that implement the interface later on. - Exception Rules: Always validate inputs to meet the interface's requirement of throwing
IllegalArgumentExceptionfor invalid cases (like null inputs).
内容的提问来源于stack exchange,提问作者Huy Tran

