0
public float sum(Object ob) {
    float sum = 0;
    for (int i = 0; i < ob.length; i++) {
        sum += ob[i];
    }

    return sum;
}

How can I implement a function that would return the sum of all the array elements of any type?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
user602774
  • 1,093
  • 7
  • 19
  • 31

3 Answers3

2

An educated guess, I havnt done java in a while. Is a cast in order?

EDIT: Or, change parameter type to float[]:

float[] ob = (float[]) ob;

public float sum(Object ob) {
    float sum = 0;
    float[] ob = (float[]) ob;
    for (int i = 0; i < ob.length; i++) {
        sum += ob[i];
    }
    return sum;

}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Bodman
  • 7,938
  • 3
  • 29
  • 34
2

What you are trying to do is ... basically ... implement a bad design. Arrays of primitives in Java are not type compatible, and you cannot do things on them in a polymorphic fashion.

It is not clear what is the best way to fix your design. But here are a couple of alternatives:

  • Write a overloaded methods for each of the cases that make sense; e.g.

    public int sum(int[] ints) ...
    
    public long sum(long[] longs) ...
    
    public float sum(float[] floats) ...
    

    Then make sure that you call them on primitive arrays whose type is known at compile time.

  • Wrap the arrays in classes, and implement the sum method for each of them:

    public interface NumberArray {
        double sum();
        ...
    }
    
    public class FloatArray {
        private float[] floats;
        public Float(float[] floats) { this.floats = floats; }
        public double sum() {
            double sum = 0.0D;
            for (float f : floats) { sum += f; }
            return sum;
        }
    }
    

    and so on.

It is actually possible to implement a float sum(Object array) method ... like this:

public float sum(Object obj) {
    float sum = 0;
    if (obj instanceof float[]) {
        for (float f : (float[]) obj) {
            sum += f;
        }
    } else if (obj instanceof int[]) {
        for (int i : (int[]) obj) {
            sum += i;
        }
    } else if ...
    } else {
        throw new SomeException("Can't sum one of these ...");
    }
    return sum;
}

but that's non-OO and (IMO) really ugly. Better to fix the design.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

As per the comment on the other answer:

java.lang.ClassCastException: [J cannot be cast to [F

Then it's a long[], not a float[]. Just use long, not float or Object. If you declare it explicitly as method argument, then you don't also need to fiddle with casts.

public long sum(long[] array) {
    long sum = 0;
    for (int i = 0; i < array.length; i++) {
        sum += array[i];
    }
    return sum;
}

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • should i be overloading methods to accept any array type. But this method would unnecessarily increase code – user602774 Mar 19 '11 at 06:29
  • Yes, you should. That's the payoff of using primitives. You're however not the only one, they also do it in standard Java SE API. Read this excellent topic: http://stackoverflow.com/questions/2337170/managing-highly-repetitive-code-and-documentation-in-java – BalusC Mar 19 '11 at 06:31