在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方法的性能。