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:
- 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 whydog(length 3) gets printed, even though it breaks the length pattern we need. - 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. - 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:
- 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 atdog(3)which is shorter thanhappy(5)). - 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
- 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
endIndexas long as this condition holds. When we hitdog(length 3, which is shorter thanhappy's length 5), we break the loop —endIndexstays at 4 (the index of "happy"). - Reverse-printing: We loop from
endIndexdown 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
相关产品推荐
相关产品推荐

