[英]How to display the Word in a Loop removing the last letter at each iteration
Problem statement:问题陈述:
Create a program that initializes a word in a character array (this becomes your stack) and displays the word removing the last letter on the stack.创建一个程序来初始化字符数组中的一个单词(这将成为您的堆栈)并显示删除堆栈中最后一个字母的单词。
Example例子
Input:输入:
LONELY
Output: Output:
LONELY
LONEL
LONE
LON
LO
L
So this is my code and I have only able to print the character on the array.所以这是我的代码,我只能打印数组上的字符。
But I haven't come up of a solution to keep removing the last letter, just like how the output would show.但是我还没有想出一个解决方案来继续删除最后一个字母,就像 output 的显示方式一样。
And probably need it to be logical.并且可能需要它是合乎逻辑的。
public static void main(String[] args) {
char word[] = {'S', 'T', 'U', 'C', 'K'};
int len = word.length;
for (int x = 0; x < len; x++) {
System.out.print(word[x]);
}
}
You can use nested for
loop.您可以使用嵌套for
循环。
The variable of the inner loop points to a particular character.内循环的变量指向一个特定的字符。 And the variable defined in the outer loop denotes the number of characters that need to be skipped in the current row.而外层循环中定义的变量表示当前行需要跳过的字符数。
Statement System.out.println()
is executed after each inner loop and advances the output to the next line. Statement System.out.println()
在每个内部循环之后执行,并将 output 推进到下一行。
public static void main(String[] args) {
char word[] = {'L', 'O', 'N', 'E', 'L', 'Y'};
int len = word.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i; j++) {
System.out.print(word[j]); // printing a row
}
System.out.println(); // advancing output to the next line
}
}
But if your challenge implies that you are expected to create an implementation of the stack data structure that will be backed by the character array, that a bit more complicated task.但是,如果您的挑战意味着您需要创建一个将由字符数组支持的堆栈数据结构的实现,那么任务就有点复杂了。
The code below is meant to provide the general idea on how to it.下面的代码旨在提供有关操作方法的一般思路。
There are three important methods that have to be present in the implementation of stack:在堆栈的实现中必须存在三个重要的方法:
push()
- adds new element ( in the method body I've provided the instructions but left out this implementation, so that you're given a chance to obtain a hands-on experience ); push()
- 添加新元素(在方法主体中我提供了说明但省略了此实现,以便您有机会获得实践经验);pop()
- removes the element from the top of the stack and returning it; pop()
- 从栈顶移除元素并返回它;peek()
- returns the element from top of the stack without removing it. peek()
- 从栈顶返回元素而不移除它。 Also, I added implementations of isEmpty()
and print()
methods that could be used to check whether the stack is empty and to print its contents, as their names suggest.此外,我还添加了isEmpty()
和print()
方法的实现,它们可用于检查堆栈是否为空并打印其内容,正如它们的名称所暗示的那样。
class CharacterStack {
private char[] letters;
private int pos = -1;
public CharacterStack(int size) { // size is passed as an argument
this.letters = new char[size]; // creating an empty array of characters
}
public CharacterStack(char[] letters) { // array of characters is passed as an argument
this.letters = letters;
this.pos = letters.length - 1; // position at the top of the stack - the greatest valid index of the array
}
public void push(char letter) {
/* to do:
1. check the length of the array
2. if current position doesn't fit in to it create a new array with greater length
3. copy the contents of the previous array into a new array
4. re-assign the variable `letters` to be a newly created array
*/
}
public char pop() { // removing the element from the top of the stack and returning it
// return letters[pos--]; a concise equivalent of the lines below
char result = letters[pos];
pos--;
return result;
}
public char peek() { // returning the element from the top of the stack without removing it
return letters[pos];
}
public boolean isEmpty() {
return pos == -1;
}
public void print() {
for (int i = 0; i <= pos; i++) {
System.out.print(letters[i]);
}
}
}
main()
- demo main()
- 演示
public static void main(String[] args) {
char[] word = {'L', 'O', 'N', 'E', 'L', 'Y'};
int len = word.length;
CharacterStack stack = new CharacterStack(word);
while (!stack.isEmpty()) {
stack.print(); // printing the contents of the stack
System.out.println(); // advancing output to the next row
stack.pop(); // removing the element from the top of the stack
}
}
Output Output
LONELY
LONEL
LONE
LON
LO
L
The Java Stack
Javadoc says (in part) Java Stack
Javadoc说(部分)
A more complete and consistent set of LIFO stack operations is provided by the
Deque
interface and its implementations, which should be used in preference to this class. For example:Deque<Integer> stack = new ArrayDeque<Integer>();
Deque
接口及其实现提供了一组更完整和一致的 LIFO 堆栈操作,应优先使用此 class。例如:Deque<Integer> stack = new ArrayDeque<Integer>();
For your case, I believe you want a Deque<Character>
.对于你的情况,我相信你想要一个Deque<Character>
。 Push all the values from the array into the Deque
and then iterate the Deque
in a nested loop.将数组中的所有值推送到双端Deque
,然后在嵌套循环中Deque
端队列。 Something like,就像是,
char[] word = "LONELY".toCharArray();
Deque<Character> stack = new ArrayDeque<>();
for (int i = 0; i < word.length; i++) {
stack.push(word[i]);
}
while (!stack.isEmpty()) {
Iterator<Character> iter = stack.descendingIterator();
while (iter.hasNext()) {
System.out.print(iter.next());
}
System.out.println();
stack.pop();
}
Which outputs (as requested)哪些输出(根据要求)
LONELY
LONEL
LONE
LON
LO
L
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.