这些代码是我在构思中写的。当我完成Deque.java时,IDE会抛出“无法解决方法进入Palindrome.java”。怎么会这样?我实际上是在Deque.java上这么做的。虽然在LinkedListDeque中继承了LinkList的get()方法。
我找到了另一个解决办法
Deque stringDeque = wordToDeque(word); 至
LinkListDeque stringDeque = (LinkListDeque) wordToDeque(word); 但我还是很好奇怎么才能找到Deque。
类图 Deque接口
/** Create an interface in a new file named Deque.
* java that contains all of the methods that appear in both ArrayDeque and LinkedListDeque.
* @param <Item>
*/
public interface Deque<Item> {
int size = 0;
/** Adds an item of type T to the front of the deque. */
void addFirst(Item item);
/** Adds an item of type T to the back of the deque. */
void addLast(Item item);
/** Returns true if deque is empty, false otherwise. */
default boolean isEmpty() {
return size == 0;
};
/** Prints the items in the deque from first to last,
* separated by a space. Once all the items have been printed, print out a new line. */
void printDeque();
/** Removes and returns the item at the front of the deque. If no such item exists, returns null. */
Item removeFirst();
/** Removes and returns the item at the back of the deque. If no such item exists, returns null. */
Item removeLast();
/** Gets the item at the given index, where 0 is the front, 1 is the next item, and so forth.
* If no such item exists, returns null. Must not alter the deque! */
Item get(int index);
}类LinkedLinkDeque.java
import java.util.LinkedList;
import java.util.NoSuchElementException;
/**
* Isn't this solution kinda... cheating? Yes.
*/
public class LinkedListDeque<Item> extends LinkedList<Item> implements Deque<Item> {
@Override
public void printDeque() {
System.out.println("dummy");
}
public Item getRecursive(int i) {
return get(i);
}
@Override
public Item removeFirst() {
try {
return super.removeFirst();
} catch (NoSuchElementException e) {
return null;
}
}
@Override
public Item removeLast() {
try {
return super.removeLast();
} catch (NoSuchElementException e) {
return null;
}
}
}Palindrome.java
import java.util.Deque;
import java.util.LinkedList;
public class Palindrome {
/** Given a String, wordToDeque should return a Deque
* where the characters appear in the same order as in the String.
* @param word
*/
public Deque<Character> wordToDeque(String word) {
Deque<Character> stringDeque = new LinkedListDeque<>();
for (String s : word.split("")) {
stringDeque.addLast(s.charAt(0));
}
return stringDeque;
}
/** Return true if the given word is a palindrome, and false otherwise. */
public boolean isPalindrome(String word) {
if (word.length() == 0 || word.length() == 1) {
return true;
}
Deque stringDeque = wordToDeque(word);
int index = word.length() / 2;
for (int i = 0; i < index; i += 1) {
if (stringDeque.get(i) != stringDeque.get(word.length() - i - 1)) { return false; }
}
return true;
}
}发布于 2020-05-24 12:32:08
您有自己的Deque接口,但也使用java.util.Deque接口。这可能会导致问题,因为接口Deque是在不同的名称空间中定义两次的。特别是在您的Palindrome.java文件中,您使用的是java.util.Deque接口,因为import java.util.Deque行位于顶部,而不是您可能预期的 Deque接口。
因此,您将得到get(int)方法丢失的错误消息。这是正确的,java.util.Deque方法并不定义get(int)方法(而是定义java.util.List )。
不要用java中已经存在的名称命名类。正如您所看到的,这可能导致命名冲突/问题。
https://stackoverflow.com/questions/61984808
复制相似问题