再不见青山 2024-08-31 17:46:50

JAVA 反转字符串的最快方法,大概比StringBuffer.reverse()性能提升5倍

public static String reverseStr(String str) {
        char[] charArray = str.toCharArray();
        int left = 0;
        int right = charArray.length - 1;
        while (left < right) {
            char temp = charArray[left];
            charArray[left] = charArray[right];
            charArray[right] = temp;
            left++;
            right--;
        }
        return new String(charArray);
}

评论

Главная - Вики-сайт
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-20 04:23
浙ICP备14020137号-1 $Гость$