【发布时间】:2020-01-13 03:15:26
【问题描述】:
所以在我的许多程序中,我在需要的任何地方都使用string.charAt() 方法,而不是将其存储在一个字符中并一次又一次地使用该字符。 charAt() 方法是否足够快,可以到处乱扔?如果你不明白我说的话,这里举个例子:
String importantStr = "Very Important String";
if(importantStr.charAt(5) == 'I') {
System.out.println(importantStr.charAt(5));
System.out.println(importantStr.charAt(5) + "Hi");
/*
Stuff to do with importantStr.charAt(5);
*/
}
我不知道你为什么要这样做,但关键是我一次又一次地打电话给importantStr.charAt(5)。将其存储到另一个角色中并使用它会节省大量时间吗?
【问题讨论】:
-
将其存储在局部变量中并使用它。
-
这可能会节省时间。但是节省的时间将是几纳秒,这通常不会很重要。