我正在编写一个使用array.length的程序。我想知道找到array.length的下划线代码是多么昂贵。我想知道继续使用array.length或创建一个int aLength = array.length并在整个程序中使用它会更好。我在网上看到一篇文章,说当使用for循环时,应该使用array.length,当不在for循环中时,aLength变量更好。再一次,在具有多个for循环的程序中使用array.length是否更好?或者设置变量并使用它更好吗?与整个string.substring(0,10)或者字符串tempString = string.subtstring(0,10)一样和tempString一样吗?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML private Label lblLeader;
File batchMarc = null;
BufferedReader bufferedreader = null;
InputStream inputstream = null;
String preProcessedRecords = null;
String[] processedRecords = null;
int counter = 0, prLength = 0;
@Override
public void initialize(URL url, ResourceBundle rb) {
try
{
batchMarc = new File("batchMarc.mrc");
bufferedreader = new BufferedReader(new FileReader(batchMarc));
String line;
while((line = bufferedreader.readLine()) != null)
{
preProcessedRecords = line;
}
processedRecords = extractIndividualRecords(preProcessedRecords);
prLength = processedRecords.length;//sets prLength to the length of he processedRecords array
for(int i = 0; i < prLength; i++)
{
System.out.println(processedRecords[i]);
}
bufferedreader.close();
}
catch (FileNotFoundException ex)
{
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex)
{
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String[] extractIndividualRecords(String preProcessed)
{
String [] processed = preProcessed.split("\\x1D");
return processed;
}
@FXML
private void handleButtonActionNext(ActionEvent event) {
if(counter < prLength && counter > -1)
{
String tempLeader = processedRecords[counter].substring(0, 24);
Platform.runLater(() -> lblLeader.setText(tempLeader));
counter++;
System.out.println(counter);
}
else if(counter == -1)
{
counter = 1;
String tempLeader = processedRecords[counter].substring(0, 24);
Platform.runLater(() -> lblLeader.setText(tempLeader));
counter++;
System.out.println(counter);
}
}
@FXML
private void handleButtonActionPrevious(ActionEvent event) {
if(counter >= 0 && counter < prLength)
{
String tempLeader = processedRecords[counter].substring(0, 24);
Platform.runLater(() -> lblLeader.setText(tempLeader));
counter--;
System.out.println(counter);
}
else if(counter == prLength)
{
counter = prLength - 2;
String tempLeader = processedRecords[counter].substring(0, 24);
Platform.runLater(() -> lblLeader.setText(tempLeader));
counter--;
System.out.println(counter);
}
}
}
答案 0 :(得分:1)
使用引用var总是更好,而不是函数调用,有一个计算涉及获取arr.length / subString(),并且必须对循环的每次迭代重复此计算。而对于参考var,此计算仅执行一次。