如何在Java中添加Integer对象和Object?

时间:2016-10-15 15:10:50

标签: java arraylist

我必须添加两个对象,一个是Integer类型,另一个是ArrayList(i)类型。这是我正在处理的函数,我需要找到数组的平均值。我得到的错误是:错误:二进制运算符的错误操作数类型' +',这里的第7行。该总和的类型为Integer,而a.get(i)的类型为Object。

 public static int brojDoProsek(ArrayList a){
    //Code here...
    double average = 0.0;
    Integer sum = new Integer(0);

    for(int i = 0; i < a.size(); i++){
        sum = sum + a.get(i);
    }

    average = sum / a.size();



    return 0;
} 

1 个答案:

答案 0 :(得分:0)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Array<E> {


public static int brojDoProsek(ArrayList a){
    //Code here...
    double average = 0.0;
    Integer sum = new Integer(0);

    for(int i = 0; i < a.size(); i++){
        //sum = sum + a.get(i);
    }

    average = sum / a.size();



    return 0;
}

public static void main(String[] args) throws IOException{
    BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); 
    String s = stdin.readLine();
    int N = Integer.parseInt(s);

    //Code here...
    ArrayList <Integer> niza = new ArrayList<Integer>(N);

    String b;
    for(int i = 0; i < N; i++){
        b = stdin.readLine();
        int temp = Integer.parseInt(b);

        niza.add(i, temp);
    }

    System.out.println(brojDoProsek(niza));     
}

}