I am writing a program that uses array.length a lot. I was wondering how costly is the underline code to find array.length. I was wondering if it would be better to continue to use array.length or create an int aLength = array.length and use it throughout the program. I read an article online that said when using for loops, array.length should be used and when not in for loops the aLength variable is better. Once again, is it better to use array.length throughout a program with many for loops or is it better to set a variable and use it? Same with something like string.substring(0,10) throughout or String tempString = string.subtstring(0, 10) once and tempString throughout?
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);
}
}
}