Java 在数组中存储计算-第二次尝试
我是Java新手,目前正在尝试学习如何在数组中最好地存储数字 我正在研究的具体问题是,通过将计算结果存储在数组中,试图找到一种更好地实现以下方法的方法 代码如下所示:Java 在数组中存储计算-第二次尝试,java,Java,我是Java新手,目前正在尝试学习如何在数组中最好地存储数字 我正在研究的具体问题是,通过将计算结果存储在数组中,试图找到一种更好地实现以下方法的方法 代码如下所示: public static long myF(int N) { long[] computedValues; computedValues = new long[N+1]; computedValues[0] = 0; computedValues[1] = 1; for (int i = 2; i <
public static long myF(int N) {
long[] computedValues;
computedValues = new long[N+1];
computedValues[0] = 0;
computedValues[1] = 1;
for (int i = 2; i < computedValues.length ;i++){
computedValues[i] = computedValues[(i-1)]+computedValues[(i-2)];
System.out.println("array["+(i)+"] = "+computedValues[i]);
}
return computedValues[N-1];
}
public static void runMyF() {
for (int N = 0; N < 100; N++)
StdOut.println(N + " " + myF(N));
}
public static void main(String[] args) {
runMyF ();
}
需要帮忙吗?
@Dukeling,你的解决方案有点超出了我的工资等级(对不起)-我认为我的代码中有一些bug,我需要帮助找到它们。谢谢。您正在将computedValues初始化为新的长字符串
computedValues = new long[N];
我想你想这样做:
computedValues[i] = F(N);
同样,在你的循环中,你没有激励i,这使得它成为无限循环。换成
for (int i = 2; i < computedValues.length ;i++)
for(int i=2;i
您忘记了数组中的初始数字:
long[] computedValues;
computedValues = new long[N];
computedValues[0] = 0;
computedValues[1] = 1;
您可以使用返回arraylist的方法:
ArrayList<Long>series=new ArrayList<Long>();
for(int i=0;i<100;i++)
{
if(i==0)series.add(new Long(0));
if(i==1)series.add(new Long(1));
if(i>1)series.add(new Long(series.get(i-1).longValue()+series.get(i-2).longValue()));
}
ArrayListseries=new ArrayList();
对于(inti=0;i1)series.add(新的Long(series.get(i-1).longValue()+series.get(i-2).longValue());
}
列表将有0,1,1,2,3,5,8,….- 您增加了错误的变量
for (int i = 2; i < computedValues.length; N++){
- 它可能应该是
computedValues=new long[N+1]代码>,否则数组太小
- 您需要返回正确的值-更改
to返回计算值[N]
返回0代码>
计算值
声明为数组列表
,并根据需要在函数中添加。这将导致您在整个程序运行期间只计算一次每个值
static ArrayList<Long> computedValues = new ArrayList<Long>(Arrays.asList(0l,1l));
public static long myF(int N) {
for (int i = computedValues.size(); i <= N; i++){
computedValues.add(computedValues.get(i-1) + computedValues.get(i-2));
System.out.println("array[" + i + "] = " + computedValues.get(i));
}
return computedValues.get(N);
}
静态ArrayList computedValues=新的ArrayList(Arrays.asList(0l,1l));
公共静态长myF(整数N){
对于(int i=computedValues.size();i,您试图做的是一种叫做记忆的事情。通常是用字典或Java中的哈希映射来完成的。可以用数组来完成,但可能有点笨重。我阅读了动态编程的相关知识,以了解如何做。F(N-2)是指2-before元素。不是N-2值。F(N-1)是指最后一个元素,不是N-1值。这个作业与递归有关吗?否则,递归确实不是生成斐波那契数的最佳方法。使用比奈近似,它表示F_N是最接近(0.5(1+SQRT(5))的整数)^如果你有一个新问题,发布一个新问题,而不是修改一个已经回答过的问题(这会使答案过时,这对那些花时间解决你的问题的人来说是不好的)。这并没有真正改变问题,我还在和回答我问题的人一起工作。谢谢……但调用runMyF()方法时仍然存在问题,不知道为什么?线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0@Snarre如果(N>0),则需要包括if
和我一样,否则当N=0
时,您将使用大小为1的computedValues
,因此computedValues[1]
将不受限制。您还需要返回computerValues[N]
(而不是N-1
)。有关工作版本,请参阅。示例中没有。如果您的代码相同,则前几行可能刚从输出中截断。关键是效率。不希望将数组中的值与效率低下的版本分开。
for (int i = 2; i < computedValues.length; i++){
computedValues[0] = 0;
if (N > 0) // needed because when N = 0, the below will be out of bounds
computedValues[1] = 1;
static ArrayList<Long> computedValues = new ArrayList<Long>(Arrays.asList(0l,1l));
public static long myF(int N) {
for (int i = computedValues.size(); i <= N; i++){
computedValues.add(computedValues.get(i-1) + computedValues.get(i-2));
System.out.println("array[" + i + "] = " + computedValues.get(i));
}
return computedValues.get(N);
}