ArrayList Java- 大小为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13355008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:23:33  来源:igfitidea点击:

ArrayList Java- Size is Zero

javaarraylist

提问by Julia Nething

My program (included below) keeps coming up with this error that the list size is zero, and that's messing up everything else. I'm guessing that means I didn't add the elements in correctly, but I can't figure out what's wrong with it. I'm technologically challenged, so I'm sorry that this isn't formatted right. Thanks for your help!

我的程序(包括在下面)不断出现列表大小为零的错误,这弄乱了其他一切。我猜这意味着我没有正确添加元素,但我无法弄清楚它有什么问题。我在技术上受到挑战,所以很抱歉格式不正确。谢谢你的帮助!

import java.util.Scanner;
import java.util.ArrayList;
public class ListStat {
    public static void main (String[] args) {
        ListStat r = new ListStat();
        System.out.println("A default list was created." + r.toString());
        ListStat r1 = new ListStat(30);
        System.out.println("Another list was created." + r1.toString());
        ListStat r2 = new ListStat(40);
        System.out.println("Another list was created." + r2.toString());
        r1.add(4); 
        System.out.println("The item at index 13 for r1 is: " + r1.get(13));
        System.out.println("r1 and r2 are equal: " + r1.equals(r2));
        System.out.println("r1 and r2 overlap this number of times: " + r1.countOverlap(r1, r2));
    }

private ArrayList<Integer> list;

public ListStat() {
list = new ArrayList<Integer>();
}

public ListStat(int N) {
list = new ArrayList<Integer>(N);
for (int i=0; i<N; i++) {
    list.add(i, i+1);
}
}

public void add(int num) {
list.add(num);
}

public int get(int index) {
return list.get(index);
}

public int getListLength(){
return list.size();
}

public double getMean() {
int sum = 0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
double mean = sum/list.size();
return mean;
}

public int getSum() {
int sum = 0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
return sum;
}

public boolean equals(ListStat r) {
if (r.equals(list))
return true;
else
return false;
}

public static int countOverlap(ListStat r1, ListStat r2) {
int count = 0;
for (int i=0; i<r1.getListLength(); i++) {
    for (int j=0; j<r2.getListLength(); j++) {
        if (r1.get(i)==r2.get(j)) {
            count++;
        }
    }
}
return count;
}

public double getStdDev() {
int sum = 0;
double standardDev = 0.0;
for (int i=0; i<list.size(); i++) {
    sum = sum + list.get(i);
}
double mean = sum/list.size();
double standsum=0;
for (int i=0; i<list.size(); i++) {
    standardDev = (list.get(i) - mean) * (list.get(i) - mean);
    standsum=standsum+standardDev;
}
standsum = standsum/list.size();
standsum = Math.sqrt(standsum);
return standsum;
}

public String toString() {
return "The list length is " + getListLength() + ", the mean is " + getMean() + ", the sum is " + getSum() + ", and the standard deviation is " + getStdDev();  
}
}

回答by Bhesh Gurung

In your main method,

在您的主要方法中,

First you invoke the no-arg constructor

首先调用无参数构造函数

ListStat r = new ListStat();    

And following is how your no-arg constructor looks like:

以下是您的无参数构造函数的样子:

public ListStat() {
    list = new ArrayList<Integer>();
}

It initializes the list but doesn't add anything to the list, so the list size is 0.

它初始化列表但不向列表添加任何内容,因此列表大小为 0。

Second, is the following

其次,是以下

System.out.println("A default list was created." + r.toString());

which is invoking the toStringmethod.

这是调用该toString方法。

If you look at the toStringmethod

如果你看toString方法

public String toString() {
    return "The list length is " + getListLength() + ", the mean is " + getMean() + ", the sum is " + getSum() + ", and the standard deviation is " + getStdDev();
}

getMeanmethod is what you are calling there, and the following is something that you are doing in that method:

getMean方法是您在那里调用的内容,以下是您在该方法中所做的事情:

double mean = sum / list.size();

The list size is 0 there.

那里的列表大小为 0。

So, you probably want to add a condition-check there, to see if the list size is 0, before doing that.

因此,您可能想在那里添加一个条件检查,以查看列表大小是否为 0,然后再这样做。

回答by Chan

The problem is caused by

问题是由

double mean = sum / list.size();

in getMean() method.

在 getMean() 方法中。

  1. The program create an object r, which is empty, means

    r.size() = 0;

  2. Then at line 9, call r.toString() method, this method invoke getMean(). Since r.size is 0, so the program throw an exception

  1. 程序创建一个对象r,它是空的,意味着

    r.size() = 0;

  2. 然后在第 9 行,调用 r.toString() 方法,该方法调用 getMean()。由于r.size为0,所以程序抛出异常

Exception in thread "main" java.lang.ArithmeticException: / by zero

线程“main”中的异常 java.lang.ArithmeticException: / 为零