【问题标题】:I want to print the arrayList in reverse by passing the arraylist to a new stack in java我想通过将arraylist传递给java中的新堆栈来反向打印arrayList
【发布时间】:2021-09-04 08:16:18
【问题描述】:

这个任务要求实现printWordRun,以便它打印从输入列表words的开头开始它可以找到的任何单词。单词 run 应该以相反的顺序打印,每个单词单独一行。 PrintWordRun 是一个方法,它接受一个名为words 的参数,它是一个ArrayList<String>。单词 run 是输入列表中的一系列单词,其中每个单词的长度都比前一个单词长。一旦我们遇到列表的末尾,或者遇到长度等于或小于前一个单词的单词,单词 run 就结束了。

数组是:


上午


开心

坐着

结果应该是:

开心


上午

要获得这项作业的全部功劳,我必须像以前一样使用堆栈来打印它,但我无法将“快乐”这个词放入堆栈。我的输出是:


上午


public class Program {
    private void printWordRun(ArrayList<String> words) {

        // Here is the code I Wrote. 
        Stack<String> wordRun = new Stack<>();

        for(int i = 1; i < words.size(); i++) {
            String str1 = words.get(i-1);
            String str2 = words.get(i);
            if(str2.length() < str1.length()) {
                break;
            }
            if(str1.length() < str2.length()){
                wordRun.push(str1);

            }


            System.out.println(wordRun);
        }
    }

    public static void main(String[] args) {
        Program program = new Program();
        program.testPrintWordRun();
    }

    private void testPrintWordRun() {
        ArrayList<String> words = new ArrayList<>();
        words.add("I");
        words.add("am");
        words.add("cat");
        words.add("with");
        words.add("happy");
        words.add("dog");
        words.add("sitting");

        System.out.println("Testing printWordRun...");
        printWordRun(words);
        System.out.println();
    }
}

【问题讨论】:

  • 哦,我忘了添加代码。刚刚编辑了原帖

标签: java arraylist data-structures collections stack


【解决方案1】:

这是构造printWordRun函数的一种方法:

Stack<String> wordRun = new Stack<>();
int maxLength = 0;

for(String s : words) {

    if(s.length() > maxLength ) {
        maxLength = s.length();
        wordRun.add(s);
    } else
        break;
}

while(!wordRun.isEmpty())
    System.out.println(wordRun.pop());

只需存储当前最大长度的值,并使用它来比较您当前的字符串。

输出:

Testing printWordRun...
happy
with
cat
am
I

【讨论】:

    【解决方案2】:

    首先使用add(int index, E element)将单词添加到堆栈中,将最后一项作为第一个插入,然后如果条件不匹配则中断循环。

    private void printWordRun(ArrayList<String> words) {
    
        // Here is the code I Wrote.
        Stack<String> wordRun = new Stack<>();
    
        for (int i = 1; i < words.size(); i++) {
            String str1 = words.get(i);
            String str2 = words.get(i - 1);
            wordRun.add(0, str2);
            if(str2.length() >= str1.length()) {
                break;
            }
        }
    
        System.out.println(wordRun); // [happy, with, cat, am, I]
    }
    

    【讨论】:

    • 好的,所以这段代码不会打印出快乐这个词。其余的填充在输出中
    • @Bobby 不,I 没有丢失。你编辑了吗?确保打印在for 循环之后。
    • 我也将“>”切换为“' 不会打印任何内容。所以我切换它并得到:with, cat, am, i
    • @Bobby 我刚刚再次测试了这段代码,它打印了[happy, with, cat, am, I]。您无需更改任何内容,只需复制过去即可。还要确保运行正确的文件。
    • 这很奇怪。谢谢。如此简单,但我很难做到。
    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多