如何在Java中对数组求和 [英] How to sum arrays in Java

查看:124
本文介绍了如何在Java中对数组求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数来在Java中对两个数组(不一定大小相等)求和并返回结果。



这是我的尝试:

  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];
}否则if(i< lhs.length){
out [i] = lhs [i];
} else / * if(i< rhs.length)* / {
out [i] = rhs [i];
}
}
退出;
}

但是我有几个观察结果,尽管有编译错误。


  1. 为什么这个函数不在Java库中,这在极端情况下是巨大的?


  2. 我有动力使用泛型,就像在C ++中使用模板一样。


  3. 我担心得到输入数据的深层副本; lhs 和``rhs。任何人都可以向我保证吗? C ++允许我传递一个常量引用;我肯定知道。


  4. 实例化 T [] out似乎是非法的通用类型。我缺少什么?


  5. 编译器会优化我重复的 Math.max(lhs.length,rhs.length)


  6. 编译器不喜欢 lhs [i] + rhs [i] 。大概是因为它不知道T的类型,但C ++允许你这样做,因为它不会尝试编译模板,直到它知道类型。


  7. <回复时会不会有深刻的副本?同样,C ++编译器也不需要额外的副本。


也许我太老了,不习惯Java; - )

解决方案


1)为什么这个函数不是在极其巨大的Java库中?


在这里询问意见,偏离主题。


3)我担心得到输入数据的深层副本; lhs和rhs。任何人都可以向我保证吗? C ++允许我传递一个常量引用;我肯定知道。



7)返回时是否要拿出一份深刻的副本?同样,C ++编译器也不需要额外的副本。


在Java中不会自动完成深度复制。此外,深度复制通常是一个定义不明确的问题。


4)实例化 T [] out对于泛型类型似乎是非法的。我缺少什么?


除了无法实例化泛型类型的数组外,泛型类型仅涵盖引用类型。你很可能只对这里的原始类型感兴趣,所以它们没用。


5)编译器会优化我重复的 Math.max(lhs.length,rhs.length)


一些JIT可能,但你不能有任何保证。提取到局部变量。


6)编译器不喜欢 lhs [i] + rhs [i] 。大概是因为它不知道 T 的类型,但是C ++允许你这样做,因为在它知道类型之前它不会尝试编译模板。


不幸的是,你在这里遇到了很多麻烦。没有办法将算法生成所有原始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;-)

解决方案

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.

这篇关于如何在Java中对数组求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆