How to sum arrays in Java

I am writing a function to sum two arrays (not necessarily the same size) in Java and return the result.

Here is 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 a few observations, despite compilation errors.

  • Why doesn't this feature exist in the Java library, which is extremely extreme?

  • I was motivated to use generics, since you would use templates in C ++.

  • I am worried about getting deep copies of the input; lhs and `` rhs. Can anyone reassure me about this? C ++ allows me to pass a permalink; and I know for sure.

  • Activating T[] out seems illegal with a generic type. What am I missing?

  • Will the compiler optimize my repeated Math.max(lhs.length, rhs.length) ?

  • The compiler does not like lhs[i] + rhs[i] . Presumably because he does not know the type of T, but C ++ allows this, since he will not try to compile the template until he knows the type.

6) Are you going to get a deep copy when you return? Again, C ++ compilers do not take an extra copy.

I may be too old to get used to Java; -)

+5
java
source share
2 answers

0) Why is this function not used in the extremely giant Java library?

Ask for an opinion, off topic here.

2) I am worried about getting deep copies of the input; lhs and rhs. Can anyone reassure me about this? C ++ allows me to pass a permalink; and I know for sure.

6) Are you going to get a deep copy when you return? Again, C ++ compilers do not take an extra copy.

No deep copying is ever done automatically in Java. In addition, deep copying is a poorly defined problem in general.

3) Running T[] out seems illegal with a generic type. What am I missing?

In addition to not being able to instantiate an array of a generic type, generic types only encompass reference types. You are probably only interested in primitive types here, so they are useless.

4) Will the compiler optimize my repeated Math.max(lhs.length, rhs.length) ?

Some JITs may, but you have no guarantee. Extract the local variable.

5) The compiler does not like lhs[i] + rhs[i] . Presumably because he does not know the type of T , but C ++ allows this, since he will not try to compile the template until he knows the type.

Unfortunately, you have a lot of problems here. It is not possible to generate algorithms for all primitive Java types.

+8
source share

5) The compiler does not like lhs [i] + rhs [i]. Presumably, because he does not know the type of T, but C ++ allows this to be done in a way that will not try to compile the template until it recognizes the type.

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

+1
source share

All Articles