在Java中,可以通過以下方式來優化charAt方法的性能:
String str = "hello";
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
// do something with c
}
String str = "hello";
int len = str.length();
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
// do something with c
}
StringBuilder sb = new StringBuilder("hello");
sb.setCharAt(0, 'H');
String result = sb.toString();
通過以上優化方法,可以提高charAt方法的性能。