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

Java入门咨询:三段if语句实现城市名称排序逻辑解析请求

Understanding the Alphabetical Sort Logic with If Statements and String Comparison

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 str1 comes after str2 alphabetically (e.g., "Zebra".compareTo("Apple") > 0)
  • Returns 0 if the strings are identical
  • Returns a negative number if str1 comes before str2 alphabetically (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:

  1. Store the original value of second in temp (so we don't lose it)
  2. Set second equal to first (now second has the "bigger" string)
  3. Set first equal to the original second value stored in temp (now first has 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;
}
  • first is "Chicago", second is "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;
}
  • second is "Chicago", third is "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;
}
  • first is "Boston", second is "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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:23:38