日期:2014-05-20  浏览次数:20642 次

类里面的方法要在一次操作中不停的调用
如 类里面有个加法运算方法 在其他类里面调用该方法时 参数值是存放在list里的 这样就要不停的重复调用该方法

Java code

public class Add{
public int getAdd(int a,int b){
int x= a+b;
return ;
}
}

参数值放在list时调用该方法,将运算后的结果放入到list里
List l = new ArrayList();
Add a = new Add();
for(int i = 0 ; i <list.size();i++){
 int m = a.getAdd(list1.get(i),list2.get(i));
 l.add(m);
}



这样写可以吗或者有什么其他的方式可以让运算速度更好点 菜鸟求教

------解决方案--------------------
哪就这样吧:
Java code

    public void getAdd(List<Integer> aList,List<Integer> bList, List<Integer> resultList){
        if((null != aList) && (null != bList)){
            int minSize = aList.size()< bList.size()?aList.size():bList.size();
            for(int i=0; i < minSize; ++i) {
                resultList.add(aList.get(i) + bList.get(i));
            }
        }
    }