Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我在这个程序中得到IndexOutOfBoundException?_Java_Arraylist_Netbeans - Fatal编程技术网

Java 为什么我在这个程序中得到IndexOutOfBoundException?

Java 为什么我在这个程序中得到IndexOutOfBoundException?,java,arraylist,netbeans,Java,Arraylist,Netbeans,这个程序使用ArrayList类,我在运行时遇到一个异常,问题是什么?我找不到任何逻辑问题 public static ArrayList<Double> differenceArray(ArrayList<Double> a, double avg) { ArrayList<Double> temp = new ArrayList<>(a.size()); for (int i = 0; i < a.size(); i++

这个程序使用ArrayList类,我在运行时遇到一个异常,问题是什么?我找不到任何逻辑问题

public static ArrayList<Double> differenceArray(ArrayList<Double> a, double avg) {

    ArrayList<Double> temp = new ArrayList<>(a.size());
    for (int i = 0; i < a.size(); i++) {
        temp.set(i, avg - a.get(i));

    }
    return temp;
}

public static void showScoresDiff(ArrayList<Double> a) {

    fillArray(a);
    double avg = computeAvg(a);
    double diff;
    for (int i = 0; i < a.size(); i++) {

        diff = avg - a.get(i);

        System.out.println("the diffrence between the avarege " + avg + " and the element " + a.get(i) + " is " + Math.abs(diff));
    }

    System.out.println("\n");

    ArrayList<Double> newArray = differenceArray(a, avg);
    for (int i = 0; i < a.size(); i++) {
        System.out.println("The " + (i + 1) + "th " + "element of the difference array is: " + newArray.get(i));
    }
}
公共静态数组列表差异数组(数组列表a,双平均值){
ArrayList temp=新的ArrayList(a.size());
对于(int i=0;i
{ 以下是输出: ]

大宗报价


以下行中存在问题:

 ArrayList<Double> temp = new ArrayList<>(a.size());
    for (int i = 0; i < a.size(); i++) {
        temp.set(i, avg - a.get(i));
现在,您可以将元素设置为索引
a.size()-1

--或--


如果您只是想在
temp
中添加一些内容,您可以使用例如
temp.add(5.5)

将一些内容添加到空列表中,而不是使用。
ArrayList<Double> temp = new ArrayList<>(a);