如何在每次迭代后将值存储在数组中?

问题描述:

考虑下面的情景:正在通过在主程序中一个for循环执行如何在每次迭代后将值存储在数组中?

Public Class A { 
..... 
static void DoSomething(){ 
    int a=some mathematical operation; 
    int b[10]; 
    } 
} 

Public Class B{ 
.... 
public static void main(String[] args) 
{ 
for(int i=0;i<10;i++){ 
A.DoSomething; 
} 
} 
} 

在这里,在A级数学运算,我想要实现的是,我要存储在计算的变量存在的价值每次迭代到数组b后的A类b。 最后,我想我的输出看起来像这样:

b[10]=[iteration1value iteration2value......iteration10value] 

给一个返回类型DoSomething()和返回值:

Public Class A { 
    ..... 
    public static int DoSomething(){ 
     int a = someMathematicalOperation(); 
     return a;  
    } 
} 

这里创建b阵列和更新的价值,你循环。

Public Class B{ 
    .... 
    public static void main(String[] args) 
    { 
     int[] b = new int[10]; 
     for(int i=0;i<10;i++){ 
      b[i] = A.DoSomething(); 
     } 
    } 
} 
+0

不知道为什么,但这给了我一个空指针异常。 – 2014-11-21 18:03:29

+0

它在哪里给你?哪条线?你可以在开场白中更新的代码中进行编辑吗? – 2014-11-21 18:17:11

+0

那么现在就解决了,我用你的逻辑,而是使用ArrayLists。谢谢。 – 2014-11-21 18:21:02

不确定您是否勾画出正确的结构。对我来说最有意义的是

1) Class A has non-static, private function called "doSomething()" which returns a result. 
2) Class B has a non-static variable which holds the array. 
3) In "main", instantiate both class A and class B. 
4) Still in main, call a.doSomething() to retrieve the result of the operation. 
5) Take the result of the operation and put it into the array held by B.