JavaRush /Java Blog /Random-TW /喝咖啡休息#115。Java 中將物件分組的簡單方法。charAt() 方法 - 如何在 Java 中使用它

喝咖啡休息#115。Java 中將物件分組的簡單方法。charAt() 方法 - 如何在 Java 中使用它

在 Random-TW 群組發布

Java 中將物件分組的簡單方法

來源:Dev.to 在這個範例中,我們將學習如何簡化包含集合作為值的Map物件的分組。 喝咖啡休息#115。 Java 中將物件分組的簡單方法。 charAt() 方法 - 如何在 Java 中使用它 - 1例如,我們有一個Map對象,其中鍵是Integer,值是字串陣列:
Grouper<Integer, String> grouper = new ArrayListGrouper<>();
        grouper.put(1, "a");
        grouper.put(1, "b");
        grouper.put(1, "c");
        grouper.put(1, "c");
        grouper.put(2, "c");
結果:
{1=[a, b, c, c], 2=[c]}
我們需要做的就是定義分組策略。例如,已經定義的ArrayListGrouper類別使用ArrayList作為其策略。我們總是可以定義一個新的Grouper來使用不同的GroupingStrateg。現在讓我們將ArrayList更改為HashSet以使元素唯一:
public class HashSetGrouper<K, V> extends Grouper<K, V> {

    public HashSetGrouper() {
        super(HashSet::new);
    }
}
然後我們測試:
@Test
    public void testHashSetGrouper() {
        Grouper<Integer, String> grouper = new HashSetGrouper<>();
        grouper.put(1, "a");
        grouper.put(1, "b");
        grouper.put(1, "c");
        grouper.put(1, "c");
        grouper.put(2, "c");
        System.out.println(grouper);

    }
結果:
{1=[a, b, c], 2=[c]}
1現在有一個集合,其中值c不重複。範例程式碼發佈在 Github 上

charAt() 方法 - 如何在 Java 中使用它

來源:FreeCodeCamp Java 中的charAt() 方法傳回字串中給定或指定索引處的char字元的值。今天我們將了解如何使用charAt()方法,從其語法開始,然後查看一些範例和用例。

如何使用 Java charAt() 方法

charAt()方法的語法如下:
public char charAt(int index)
請注意,使用charAt()方法 從字串傳回的字元屬於char資料類型。稍後我們將看到這如何影響傳回值的串聯。現在讓我們來看看範例:
public class Main {
  public static void main(String[] args) {

    String greetings = "Hello World";

    System.out.println(greetings.charAt(0));
    // H
  }
}
在上面的程式碼中,我們的字串儲存在名為greetings 的變數中,內容為「Hello World」。我們使用charAt()方法來取得索引 0 處的字元。該字元是字母 H。第一個字元始終位於索引 0 處,第二個字元位於索引 1 處,依此類推。子字串之間的空格也被視為索引。在下面的範例中,我們將看到當我們嘗試連接不同的返回字元時會發生什麼。連接意味著將兩個或多個值連接在一起(在大多數情況下,該術語用於連接字串中的字元或子字串)。
public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    char ch1 = greetings.charAt(0); // H
    char ch2 = greetings.charAt(4); // o
    char ch3 = greetings.charAt(9); // l
    char ch4 = greetings.charAt(10); // d

    System.out.println(ch1 + ch2 + ch3 + ch4);
    // 391
  }
}
使用charAt()方法,我們得到索引為 0、4、9 和 10 的字符,分別是字母 H、o、l 和 d。然後我們嘗試列印並連接這些字元:
System.out.println(ch1 + ch2 + ch3 + ch4);
但我們沒有回傳“Hold”,而是得到了 391。這是因為傳回的值不再是字串,而是char類型。因此,當我們連接它們時,解釋器會添加它們的 ASCII 值。H 的 ASCII 值為 72,o 的值為 111,l 的值為 108,d 的值為 100。如果將它們加在一起,我們會得到 391,這在上一個範例中傳回。

錯誤 StringIndexOutOfBoundsException

當我們傳遞的序號大於字串中的字元數時,我們會在控制台中收到 StringIndexOutOfBoundsException 錯誤。此錯誤也適用於使用負索引,而 Java 不支援負索引。在Python等支援負索引的程式語言中,傳遞-1將為您提供資料集中的最後一個字元或值,類似於0總是傳回第一個字元。這是一個例子:
public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    char ch1 = greetings.charAt(20);

    System.out.println(ch1);

    /* Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 20
    */
  }
}
在上面的程式碼中,我們傳遞了索引 20:char ch1 =greetings.charAt(20); ,它超出了greetings變數中的字元數,所以我們得到了一個錯誤。您可以在上面的程式碼區塊中看到註解掉的錯誤訊息。同樣,如果我們傳遞一個負值,如下所示:char ch1 =greetings.charAt(-1); ,然後我們會得到類似的錯誤。

結論

在本文中,我們學習如何在 Java 中使用charAt()方法。我們了解如何根據索引號返回字串中的字符,以及連接這些字符時會發生什麼。我們也討論了在 Java 中 使用charAt()方法時可能會收到錯誤回應的一些情況。
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION