繁体   English   中英

如何在循环中显示单词,在每次迭代时删除最后一个字母

[英]How to display the Word in a Loop removing the last letter at each iteration

问题陈述:

创建一个程序来初始化字符数组中的一个单词(这将成为您的堆栈)并显示删除堆栈中最后一个字母的单词。

例子

输入:

LONELY

Output:

LONELY
LONEL
LONE
LON
LO
L

所以这是我的代码,我只能打印数组上的字符。

但是我还没有想出一个解决方案来继续删除最后一个字母,就像 output 的显示方式一样。

并且可能需要它是合乎逻辑的。

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]);
    }
}

您可以使用嵌套for循环。

内循环的变量指向一个特定的字符。 而外层循环中定义的变量表示当前行需要跳过的字符数。

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
    }
}

但是,如果您的挑战意味着您需要创建一个将由字符数组支持的堆栈数据结构的实现,那么任务就有点复杂了。

下面的代码旨在提供有关操作方法的一般思路。

在堆栈的实现中必须存在三个重要的方法:

  • push() - 添加新元素(在方法主体中我提供了说明但省略了此实现,以便您有机会获得实践经验);
  • pop() - 从栈顶移除元素并返回它;
  • peek() - 从栈顶返回元素而不移除它。

此外,我还添加了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() - 演示

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

LONELY
LONEL
LONE
LON
LO
L

Java Stack Javadoc说(部分)

Deque接口及其实现提供了一组更完整和一致的 LIFO 堆栈操作,应优先使用此 class。例如: Deque<Integer> stack = new ArrayDeque<Integer>();

对于你的情况,我相信你想要一个Deque<Character> 将数组中的所有值推送到双端Deque ,然后在嵌套循环中Deque端队列。 就像是,

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();
}

哪些输出(根据要求)

LONELY
LONEL
LONE
LON
LO
L

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM