免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2061 | 回复: 3
打印 上一主题 下一主题

通用形式或通配符形式求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-27 23:04 |只看该作者 |倒序浏览
我想计算两个数组的均值和方差,但一个是整型数组,另一个是double 型。这两个数组的值是从相应的linklist 里推倒出来的,我用了一个较笨的办法,即用了两个函数来计算。


  1. package linklist;

  2. public class processing {
  3.    private String arrayName;
  4.    private int array1[];
  5.    private double array2[];
  6.    
  7.    //constructor initializes array
  8.    public processing(String name,int a1[],double a2[])
  9.    {
  10.        arrayName = name; // initialize arrayName
  11.        array1 = a1;
  12.        array2 = a2;// store array
  13.    } // end two-argument processing constructor
  14.    
  15.   
  16.    

  17. ...   
  18.    //find the mean of array1
  19.    public double getMean1()
  20.    {
  21.        double m1 = 0;
  22.        // loop through array1
  23.       
  24.         for (int c1 = 0; c1 < array1.length; c1++)
  25.         { m1 = m1 + array1[c1];
  26.        System.out.printf("%d\n",array1[c1]);}
  27.          m1 = m1/array1.length; // average the sum of the array
  28.       
  29.          return m1;
  30.    } // end method getMean1
  31.    
  32.    
  33.    public double getMean2()
  34.    {
  35.        double m1 = 0;
  36.        // loop through array2
  37.       
  38.         for (int c1 = 0; c1 < array2.length; c1++)
  39.         { m1 = m1 + array2[c1];
  40.        System.out.printf("%6.2f\n",array2[c1]);}
  41.          m1 = m1/array2.length; // average the sum of the array
  42.       
  43.          return m1;
  44.    } // end method getMean2
  45. }
复制代码

显然这个办法不灵活,我的第一个问题是怎么将getMean1 和 getMean2可以合并。

第二个问题是
可不可以从linklist提取数据像下面的形式


  1. import java.lang.Integer;
  2. import java.lang.Double;
  3. public class ListTest
  4. {
  5.       public static void main( String args[])
  6.       {
  7.              List list1 = new List();
  8.              List list2 = new List();//create the List container
  9.              // initializer list specifies the value for each element
  10.              int array1[] = { 160, 591, 114, 229, 230, 270, 128, 1657, 624, 1503 };
  11.              double array2[] = { 15, 69.9, 6.5, 22.4, 28.4, 65.9, 19.4, 198.7, 38.8, 138.2 };
  12.             
  13.              Integer[] b1;
  14.              Double[] b2;
  15.              //insert numbers in list
  16.              for (int c1 = 0; c1 < array1.length; c1++ )
  17.                  list1.insertAtFront(array1[ c1 ]);
  18.              for (int c2 = 0; c2 < array2.length; c2++)
  19.                  list2.insertAtFront(array2[ c2 ]);
  20.              for (int c1 = 0; c1 < array1.length; c1++ )
  21.              {
  22.                  Object removeObject = list1.removeFromFront();
  23.                          Integer x = (Integer)removeObject;
  24.                          b1 [ c1 ] =  x.intValue();

  25.              }
  26.              for (int c2 = 0; c2 < array2.length; c2++ )
  27.              {
  28.                  Object removeObject = list2.removeFromFront();
  29.                  Double y = (Double)removeObject;
  30.                  b2 [ c2 ] = y.doubleValue();
  31.              }
复制代码


请注意code


  1.    Integer[] b1;
  2. ....
  3. // then
  4. Integer x = (Integer)removeObject;
  5.                          b1 [ c1 ] =  x.intValue();
复制代码

万分感谢

论坛徽章:
0
2 [报告]
发表于 2007-01-29 21:13 |只看该作者
【1】 getMean1()和getMean2()可以用java 5引入的泛型编程来合并成一个方法。
http://www.ibm.com/developerworks/cn/java/j-djc02113/

【2】 LinkedList应该和List接口的行为都是一致的。

论坛徽章:
0
3 [报告]
发表于 2007-02-03 22:22 |只看该作者
能不能详细点,刚开始学,需要手把手教。
那些定义实在看起来费劲。
最好,能把代码写下来,以后就能举一反三,再去反过来理解定义,可能会更清楚些。
帮个忙吧,万分感谢。

论坛徽章:
0
4 [报告]
发表于 2007-02-04 08:23 |只看该作者
泛型好像不支持基本类型吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP