问题
I am currently taking a Java Programming class and we are on the topic of dynamic arrays. We were asked to write a program using dynamic arrays that would print out the Fibonacci Sequence.
This is what I have:
public class Fibonacci {
private static int[] data;
public static void DynamicArray() {
data = new int[1];
}
public static int get(int position) {
if (position >= data.length){
return 0;
} else {
return data[position];
}
}
public static void put(int position, int value) {
if(position >= data.length) {
int newSize = 2 * position;
int[] newData = new int[newSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
data[position] = value;
}
public static void main(String[] args) {
System.out.println("\nFibonacci Sequence:\n");
System.out.println(data[0]);
for(int i = 2; i< = 20; i++) {
data[i] = data[i-1] + data[i-2];
System.out.println(data[i]);
}
}
}
Thanks!
回答1:
Does it counts as dynamic array if you use the .push() method? If it's OK you can just count the next number in Fibonacci Sequence and use MyArray.push(currentFibonacci);
回答2:
In Java, Arrays are static. Once they are initialised, they can not 'grow'. There are several data structures for solving this problem (linked lists for example).
If you want to resize an array, you'll have to create a new one with the wanted size and then copy all entrys from the old array to the new one. Java's implementation of this kind of "dynamic array" is the ArrayList. Yet this is not very fast
回答3:
I had to do the same thing don't worry its a lot simpler then you are making it look at the code I have that does what you want
import java.util.Scanner;
public class DynamicArray
{
public static void
d main (String[] args){
System.out.println("How Long Do You Want To see the Fibonacci Series?");
Scanner scan = new Scanner (System.in);
int Length = scan.nextInt();
int[] Fibonacci = new int[Length];
Fibonacci[0] = 0;
Fibonacci[1] = 1;
System.out.println("Fibonacci Series");
System.out.println(Fibonacci[0]);
for (int i = 2; i<Length; i++){
Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1];
System.out.println(Fibonacci[i]);
}
}
}
So what is happening here is that the length variable I declared is inputted by the user determining how far the array will print the Fibonacci series it is rather simple once you know what you are trying to do. So this is what you need since this is what your teacher wants hope it helps. :D
回答4:
while (sequence<4000000)
{
i++;
sequence = fibonacciSequence[0]+fibonacciSequence[1];
System.out.println(sequence);
if (i%2!=0)
{
fibonacciSequence[0]=sequence;
}
else
{
fibonacciSequence[1]=sequence;
}
if (sequence%2==0)
{
sumEvenTerm = sumEvenTerm + sequence;
}
}
@Randy thanks for your answer here. In this case, we do know the length of the fibonnaci sequence. In some cases, we do not know the length. What we know is the value of the some fibonnaci number. We can't use the above program.
You can change the value in the while loop to suit your needs.
来源:https://stackoverflow.com/questions/14864815/how-to-use-java-dynamic-array