错误:变量可能尚未初始化 - 如何初始化ArrayList?

时间:2018-03-02 15:59:23

标签: java arraylist

我已经审核了所有之前的问题,其标题与此类似,但我无法找到解决方案。所有错误都表明我没有初始化ArrayList ..我是不是用import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.io.PrintWriter; public class main { public static void main (String[] args) throws FileNotFoundException { ArrayList<Double> RPM, bCoeffs, filteredRPM = new ArrayList<Double>(); Scanner RPMFile = new Scanner(new File("RotationSpeed.txt")); while(RPMFile.hasNextLine()){ String line = RPMFile.nextLine(); Scanner scanner = new Scanner(line); scanner.useDelimiter(","); while(scanner.hasNextDouble()){ RPM.add(scanner.nextDouble()); } scanner.close(); } RPMFile.close(); int windowSize = 10; int filterItterations = 1; for (int i = 0; i < windowSize; i++){ double temp = 1/windowSize; bCoeffs.add(temp); } for (int k = 1; k <= filterItterations; k++){ if (k == 1){ for (int n = windowSize; n < RPM.size(); n++){ int m = 0; double tempYSum = 0; for (int j = 0; j < windowSize; j++){ double tempY = (bCoeffs.get(j))*(RPM.get(n-m)); tempYSum += tempY; m++; } filteredRPM.add(tempYSum); } }else{ int i = 1; for (int n = windowSize; n < filteredRPM.size(); n++){ int m = 0; double tempYSum = 0; for (int j = 0; j < windowSize; j++){ double tempY = (bCoeffs.get(j))*(filteredRPM.get(n-m)); tempYSum += tempY; m++; } filteredRPM.set(i, tempYSum); i++; } } } } } 初始化ArrayList?

main.java:20: error: variable RPM might not have been initialized
                RPM.add(scanner.nextDouble());
                ^
main.java:31: error: variable bCoeffs might not have been initialized
                                bCoeffs.add(temp);
                                ^
main.java:36: error: variable RPM might not have been initialized
                                for (int n = windowSize; n < RPM.size(); n++){
                                                             ^
main.java:40: error: variable bCoeffs might not have been initialized
                                                double tempY = (bCoeffs.get(j))*(RPM.get(n-m));
                                                                ^
main.java:52: error: variable bCoeffs might not have been initialized
                                                double tempY = (bCoeffs.get(j))*(filteredRPM.get(n-m));
                                                                ^
5 errors

我收到的错误如下:

function displayNotification() {
  if (Notification.permission == 'granted') {
    navigator.serviceWorker.getRegistration().then(function(reg) {
      var options = {
        body: 'Welcome!',
        icon: '/css/logo.png',
        vibrate: [100, 50, 100],
        data: {
          dateOfArrival: Date.now(),
          primaryKey: 1
        }
      };
      reg.showNotification('My name', options);
    });
  }
}


navigator.serviceWorker.register('/service-worker.js');
Notification.requestPermission(function(result) {
  if (result === 'granted') {
    navigator.serviceWorker.ready.then(function(registration) {
      displayNotification();
    });
  }
});

2 个答案:

答案 0 :(得分:3)

只需分配变量:您当前正在声明3个变量,但只为最后一个变量赋值。

ftdi_instance.open_from_url()

请注意,某些人不鼓励在同一行声明许多变量,例如: https://vue-test-utils.vuejs.org/en/api/wrapper/setData.html:如果每行执行一次,则更容易阅读:

ArrayList<Double> RPM = new ArrayList<>(), bCoeffs = new ArrayList<>(), filteredRPM = new ArrayList<>();

答案 1 :(得分:1)

虽然你的第一行可能看起来像初始化所有三个ArrayLists,但它基本上会执行以下操作:

ArrayList<Double> RPM;
ArrayList<Double> bCoeffs;
ArrayList<Double> filteredRPM = new ArrayList<Double>();

如果它按照您可能的方式工作,但您会创建许多需要澄清的潜在问题。 例如:现在所有三个变量都指向相同的内存(换句话说是同一个ArrayList)?或者它们都指向不同的ArrayList。

有一个原因是不鼓励在一行中初始化几个变量,因为它非常混淆。

你这样做的正确方法是:

ArrayList<Double> RPM = new ArrayList<>();
ArrayList<Double> bCoeffs = new ArrayList<>();
ArrayList<Double> filteredRPM = new ArrayList<>();