如何附加堆叠字符值
How to append stacked character values
我需要帮助将推送的元素附加到堆栈中。我想要下面的 returnItems 方法 return 将推入的元素放到堆栈上,如果它是回文,将用于与下面的字符串比较。
此字符串的每个字符都被压入堆栈:abcdef
这是 returnItems 方法。如何修改粗体部分以获得 return 值(例如:上例中的 fedcba):
public T returnItems() {
Node<T> temp = top;
T value = null;
if (top == null) { // checks if stack is empty
System.out.println("Stack is empty");
value = null;
}
System.out.println("Elements: ");
while (temp.getInfo() != null) {
value = temp.getInfo(); // get the current character
// How do I append the characters that the value variable temporarily holds
// for each loop
***value = (T) (value + " " + temp.getLink());*** // append it to the current character
if (temp.getLink() == null) { // if the next link is null, the loop will break
break;
}
temp = temp.getLink(); // else, get the next link
}
return value;
}
当您对堆栈上的两个元素使用 T(对于 char
可能是 Character
)和结果 String
,必须假设您有一个 Stack<String>
左右,因此 Node<String>
.
public String returnItems() {
String reversed = ""; // Better a StringBuilder.
while (top != null) {
String ch = top.getInfo(); // get the current character
reversed = reversed + ch;
top = top.getLink(); // or `pop()` or such.
}
return reversed;
}
因此:使用具体类型,而不是 T。这意味着 returnItems
必须 而不是 在堆栈 class 本身中,因为回文是仅限字符堆栈(堆栈用法)。我希望看到:
class Stack<T> {
T top() { ... }
boolean isEmpty() { ... }
T pop() { ... }
void push(T item) { ... }
}
Stack<Character> charStack = new Stack<>();
public String returnItems() {
String reversed = ""; // Better a StringBuilder.
while (!charStack.isEmpty()) {
char ch = charStack.pop(); // get the current character
reversed += ch;
}
return reversed;
}
我需要帮助将推送的元素附加到堆栈中。我想要下面的 returnItems 方法 return 将推入的元素放到堆栈上,如果它是回文,将用于与下面的字符串比较。
此字符串的每个字符都被压入堆栈:abcdef
这是 returnItems 方法。如何修改粗体部分以获得 return 值(例如:上例中的 fedcba):
public T returnItems() {
Node<T> temp = top;
T value = null;
if (top == null) { // checks if stack is empty
System.out.println("Stack is empty");
value = null;
}
System.out.println("Elements: ");
while (temp.getInfo() != null) {
value = temp.getInfo(); // get the current character
// How do I append the characters that the value variable temporarily holds
// for each loop
***value = (T) (value + " " + temp.getLink());*** // append it to the current character
if (temp.getLink() == null) { // if the next link is null, the loop will break
break;
}
temp = temp.getLink(); // else, get the next link
}
return value;
}
当您对堆栈上的两个元素使用 T(对于 char
可能是 Character
)和结果 String
,必须假设您有一个 Stack<String>
左右,因此 Node<String>
.
public String returnItems() {
String reversed = ""; // Better a StringBuilder.
while (top != null) {
String ch = top.getInfo(); // get the current character
reversed = reversed + ch;
top = top.getLink(); // or `pop()` or such.
}
return reversed;
}
因此:使用具体类型,而不是 T。这意味着 returnItems
必须 而不是 在堆栈 class 本身中,因为回文是仅限字符堆栈(堆栈用法)。我希望看到:
class Stack<T> {
T top() { ... }
boolean isEmpty() { ... }
T pop() { ... }
void push(T item) { ... }
}
Stack<Character> charStack = new Stack<>();
public String returnItems() {
String reversed = ""; // Better a StringBuilder.
while (!charStack.isEmpty()) {
char ch = charStack.pop(); // get the current character
reversed += ch;
}
return reversed;
}