- 论坛徽章:
- 0
|
我想计算两个数组的均值和方差,但一个是整型数组,另一个是double 型。这两个数组的值是从相应的linklist 里推倒出来的,我用了一个较笨的办法,即用了两个函数来计算。
如
- package linklist;
- public class processing {
- private String arrayName;
- private int array1[];
- private double array2[];
-
- //constructor initializes array
- public processing(String name,int a1[],double a2[])
- {
- arrayName = name; // initialize arrayName
- array1 = a1;
- array2 = a2;// store array
- } // end two-argument processing constructor
-
-
-
- ...
- //find the mean of array1
- public double getMean1()
- {
- double m1 = 0;
- // loop through array1
-
- for (int c1 = 0; c1 < array1.length; c1++)
- { m1 = m1 + array1[c1];
- System.out.printf("%d\n",array1[c1]);}
- m1 = m1/array1.length; // average the sum of the array
-
- return m1;
- } // end method getMean1
-
-
- public double getMean2()
- {
- double m1 = 0;
- // loop through array2
-
- for (int c1 = 0; c1 < array2.length; c1++)
- { m1 = m1 + array2[c1];
- System.out.printf("%6.2f\n",array2[c1]);}
- m1 = m1/array2.length; // average the sum of the array
-
- return m1;
- } // end method getMean2
- }
复制代码
显然这个办法不灵活,我的第一个问题是怎么将getMean1 和 getMean2可以合并。
第二个问题是
可不可以从linklist提取数据像下面的形式
- import java.lang.Integer;
- import java.lang.Double;
- public class ListTest
- {
- public static void main( String args[])
- {
- List list1 = new List();
- List list2 = new List();//create the List container
- // initializer list specifies the value for each element
- int array1[] = { 160, 591, 114, 229, 230, 270, 128, 1657, 624, 1503 };
- double array2[] = { 15, 69.9, 6.5, 22.4, 28.4, 65.9, 19.4, 198.7, 38.8, 138.2 };
-
- Integer[] b1;
- Double[] b2;
- //insert numbers in list
- for (int c1 = 0; c1 < array1.length; c1++ )
- list1.insertAtFront(array1[ c1 ]);
- for (int c2 = 0; c2 < array2.length; c2++)
- list2.insertAtFront(array2[ c2 ]);
- for (int c1 = 0; c1 < array1.length; c1++ )
- {
- Object removeObject = list1.removeFromFront();
- Integer x = (Integer)removeObject;
- b1 [ c1 ] = x.intValue();
- }
- for (int c2 = 0; c2 < array2.length; c2++ )
- {
- Object removeObject = list2.removeFromFront();
- Double y = (Double)removeObject;
- b2 [ c2 ] = y.doubleValue();
- }
复制代码
请注意code
- Integer[] b1;
- ....
- // then
- Integer x = (Integer)removeObject;
- b1 [ c1 ] = x.intValue();
复制代码
万分感谢 |
|