How to clean up my code

Posted by simion on Stack Overflow See other posts from Stack Overflow or by simion
Published on 2010-06-14T13:52:48Z Indexed on 2010/06/14 14:02 UTC
Read the original article Hit count: 162

Filed under:
|
|

Being new to this i realy am trying to learn how to keep code as simple as possible, whilst doing the job its supposed to.

The question i have done is from project eulur, it says

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

Here is my code below, i was wondering what the best way of simplifying this would be, for a start removing all of the .get(list.length()-1 )..... stuff would be a good start if possible but i dont really no how to?

Thanks

public long fibb()
{
    ArrayList<Integer> list = new ArrayList<Integer>();


    list.add(1);
    list.add(2);

    while((list.get(list.size() - 1) +  (list.get(list.size() - 2)) < 4000000)){  
        list.add((list.get(list.size()-1)) + (list.get(list.size() - 2)));
    }     

    long value = 0;
    for(int i = 0; i < list.size(); i++){
        if(list.get(i) % 2 == 0){
            value += list.get(i);
        }    
    }
    return value;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about simple