,我越来越如何解决数组索引超出界限的错误?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 610
at Fib.sorted(Fib.java:67)
at Fib.main(Fib.java:17)
我的代码
public class Fib
{
public static void main(String args[])
{
System.out.println(Arrays.toString(fiblist));
System.out.println(Fib.add());
System.out.println(Fib.square());
System.out.println(Fib.reversal());
System.out.println(Fib.sorted());
}
public static int fiblist[] = {1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765};
public static int fiblen = fiblist.length;
public Fib()
{
// Do nothing
}
public static ArrayList<Integer> sorted()
{
ArrayList sorted = new ArrayList();
for(int counter = 0; counter < fiblist[4]; counter++)
{
int temp1 = fiblist[counter];
System.out.println("Elements stored " + temp1);
}
for(int counter = fiblist[14]; counter < fiblist[19]; counter++)
{
int temp2 = fiblist[counter];
System.out.println("Last Elements stored " + temp2);
}
return sorted;
}
}
我想我的数组的最后5个元素存放在温度2 然后我会切换他们的错误。 有没有更简单的方法来做到这一点? 用最后五个数组切换数组的前五个元素? 你将如何使用for循环切换它们?
你'counter'应该是在数组中的位置,而不是价值那个位置。将counter
2014-10-01 19:39:32
这看起来像麻烦'int counter = fiblist [14];计数器
gtgaxiola
2014-10-01 19:39:47
而且'counter
2014-10-01 19:40:08