You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java ArrayList逆序打印长度递增连续序列的代码问题排查

Fixing the Reverse-Print Logic for Length-Constrained Sequence

Let's break down what's wrong with your current code and fix it to match the expected output of happy with cat am I.

What's Wrong with the Original Code?

Your printWordRun method has three critical issues that cause it to print dog happy with cat am I instead of the desired output:

  1. Incorrect condition check: The line if(str.length() < words.size()) compares each element's length to the total number of elements in the list (which is 7 here) — this has nothing to do with checking if the element fits the required length sequence. That's why dog (length 3) gets printed, even though it breaks the length pattern we need.
  2. Incomplete loop range: The loop uses i > 0, which stops at index 1 (the element "am") and skips index 0 ("I"). While your actual output includes "I", this suggests a possible typo, but we'll fix it to include all relevant elements regardless.
  3. No sequence break detection: The code doesn't check if the length sequence is broken, so it prints every element that passes the incorrect length check.

The Correct Logic to Implement

The task requires us to:

  1. Identify the longest prefix of the list where each element's length is strictly increasing (since I(1) < am(2) < cat(3) < with(4) < happy(5) — this sequence breaks at dog(3) which is shorter than happy(5)).
  2. Reverse-print this valid prefix (so we get happy with cat am I).

Fixed Code

import java.util.ArrayList;

public class Program {
    private void printWordRun(ArrayList<String> words) {
        // First, find the end of the longest increasing length prefix
        int endIndex = 0;
        for (int i = 1; i < words.size(); i++) {
            String current = words.get(i);
            String previous = words.get(i - 1);
            if (current.length() > previous.length()) {
                endIndex = i;
            } else {
                // Sequence is broken, stop checking further
                break;
            }
        }

        // Now reverse-print from endIndex down to 0
        for (int i = endIndex; i >= 0; i--) {
            System.out.print(words.get(i));
            // Add a space between elements, except after the last one
            if (i != 0) {
                System.out.print(" ");
            }
        }
        System.out.println();
    }

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

How This Works

  1. Finding the valid prefix: We loop through the list starting from index 1, checking if each element's length is longer than the previous one. We update endIndex as long as this condition holds. When we hit dog (length 3, which is shorter than happy's length 5), we break the loop — endIndex stays at 4 (the index of "happy").
  2. Reverse-printing: We loop from endIndex down to 0, printing each element. We add spaces between elements to match the expected output format.

When you run this code, it will output exactly happy with cat am I as desired.

内容的提问来源于stack exchange,提问作者Bobby

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:52:35