Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum arrays in Java

Tags:

java

I'm writing a function to sum two arrays (of not necessarily equal size) in Java and return the result.

Here's my attempt:

 public static <T> T[] sumArrays(T[] lhs, T[] rhs)
    {
        T[] out = new T[Math.max(lhs.length, rhs.length)];

        for (int i = 0; i < Math.max(lhs.length, rhs.length); ++i){            
            if (i < Math.min(lhs.length, rhs.length)){
                out[i] = lhs[i] + rhs[i];                
            } else if (i < lhs.length){
                out[i] = lhs[i];
            } else /* if (i < rhs.length)*/{
                out[i] = rhs[i];
            }            
        }
        return out;        
    }

But I have several observations notwithstanding the compile errors.

  1. Why isn't this function in the Java library, which is gigantic in the extreme?

  2. I was motivated to use generics, as you would use templates in C++.

  3. I'm worried about getting deep copies of the input data; lhs and ``rhs. Can anyone reassure me on this? C++ allows me to pass a constant reference; and I'll know for sure.

  4. Instantiation of T[] out appears to be illegal with a generic type. What am I missing?

  5. Will the compiler optimise out my repeated Math.max(lhs.length, rhs.length)?

  6. Compiler doesn't like lhs[i] + rhs[i]. Presumably because it doesn't know the type of T, but C++ allows you do to this as it will not attempt to compile a template until it knows the type.

  7. Are going to take a deep copy of out when returning? Again, C++ compilers would not take an extra copy.

Perhaps I'm too old to get used to Java;-)

like image 984
Bathsheba Avatar asked Dec 26 '22 01:12

Bathsheba


2 Answers

1) Why isn't this function in the extremely gigantic Java library?

Asking for opinion, off-topic here.

3) I'm worried about getting deep copies of the input data; lhs and rhs. Can anyone reassure me on this? C++ allows me to pass a constant reference; and I'll know for sure.

7) Are going to take a deep copy of out when returning? Again, C++ compilers would not take an extra copy.

No deep copying is ever done automatically in Java. Furthermore, deep copying is an ill-defined problem in general.

4) Instantiation of T[] out appears to be illegal with a generic type. What am I missing?

Apart from it being impossible to instantiate an array of generic type, generic types only cover reference types. You are quite probably interested only in primitive types here, so they are of no use.

5) Will the compiler optimise out my repeated Math.max(lhs.length, rhs.length)?

Some JITs might, but you can't have any kind of guarantee. Extract to a local variable.

6) Compiler doesn't like lhs[i] + rhs[i]. Presumably because it doesn't know the type of T, but C++ allows you do to this as it will not attempt to compile a template until it knows the type.

Unfortunately, you are in a lot of trouble here. There is no way to generify algorithms to all primitive Java types.

like image 73
Marko Topolnik Avatar answered Jan 10 '23 15:01

Marko Topolnik


6) Compiler doesn't like lhs[i] + rhs[i]. Presumably because it doesn't know the type of T, but C++ allows you do to this as it will not attempt to compile a template until it knows the type.

You could always write an interface with an .add(...) function and let T extend this interface. Then you could write lhs[i].add(rhs[i])

like image 29
morrow Avatar answered Jan 10 '23 15:01

morrow