Java入门咨询:三段if语句实现城市名称排序逻辑解析请求
Hey there! Let's break down this code piece by piece so you can wrap your head around how it sorts those city names, step by step. I'll cover the String.compareTo() method, the variable swap logic, and why those first/last if blocks are identical.
First: What does String.compareTo() do?
Before diving into the if blocks, you need to grasp this method—it's the core of the sorting here. When you call str1.compareTo(str2):
- It returns a positive number if
str1comes afterstr2alphabetically (e.g., "Zebra".compareTo("Apple") > 0) - Returns 0 if the strings are identical
- Returns a negative number if
str1comes beforestr2alphabetically (e.g., "Apple".compareTo("Zebra") < 0)
So in the code, if(first.compareTo(second)>0) is checking: "Is first alphabetically later than second?" If yes, we need to swap them to get them in order.
How the Variable Swap Works
Let's look at the swap logic inside the if blocks:
temp = second; second = first; first = temp;
Think of temp as a temporary "holding box"—we need it because if we just did second = first first, we'd lose the original value of second forever. Here's the play-by-play:
- Store the original value of
secondintemp(so we don't lose it) - Set
secondequal tofirst(nowsecondhas the "bigger" string) - Set
firstequal to the originalsecondvalue stored intemp(nowfirsthas the "smaller" string)
This safely swaps the two variables without data loss.
Walking Through Each If Block
Let's use a concrete example to see how the three if blocks work together. Suppose we input the cities: Chicago, Boston, Atlanta
1. First If Block: Sort first and second
if(first.compareTo(second)>0) { temp = second; second = first; first = temp; }
firstis "Chicago",secondis "Boston"- "Chicago".compareTo("Boston") > 0 (C comes after B), so we swap them
- Now:
first = "Boston",second = "Chicago",third = "Atlanta"
2. Second If Block: Sort second and third
if(second.compareTo(third) > 0) { temp = third; third = second; second = temp; }
secondis "Chicago",thirdis "Atlanta"- "Chicago".compareTo("Atlanta") > 0 (C comes after A), so we swap them
- Now:
first = "Boston",second = "Atlanta",third = "Chicago"
Wait a minute—now first ("Boston") is alphabetically later than second ("Atlanta")! That's a problem we didn't have before the second swap. That's where the third if block comes in.
3. Third If Block: Fix first and second Again
if(first.compareTo(second) > 0) { temp = second; second = first; first = temp; }
firstis "Boston",secondis "Atlanta"- "Boston".compareTo("Atlanta") > 0 (B comes after A), so we swap them
- Now:
first = "Atlanta",second = "Boston",third = "Chicago"
Perfect—now all three are in alphabetical order!
Why the First and Third If Blocks Are Identical
The second if block can break the order we established between first and second (like in our example, swapping second and third turned second into a smaller string than first). The third if block re-checks and fixes that relationship, ensuring first ≤ second ≤ third by the end.
Without that third check, our example would have output "Boston Atlanta Chicago"—which is not fully sorted.
Wrap-Up
This is a simplified, manual sorting approach for exactly three elements. It's like a tiny bubble sort pass: we first fix the first pair, then the second pair, then double-check the first pair in case the second swap messed it up. Even though using Arrays.sort() would be easier, understanding this manual logic is great for building a foundation in how sorting works under the hood.
内容的提问来源于stack exchange,提问作者Janeson00

