Search code examples
javaarraysfor-looparraylisttraversal

How do I successfully traverse a pre-filled ArrayList to find the difference of each consecutive element and print it out?


public class AccumulatedData {

    public static void main(String args[]) {

        ArrayList<Double> weights = new ArrayList<Double>();

        weights.add(145.0);
        weights.add(146.5);
        weights.add(146.5);
        weights.add(147.0);
        weights.add(146.0);
        weights.add(148.0);
        weights.add(148.5);

        ArrayList<Double> printWeightChanges = getWeightChanges(weights);
        System.out.println(weights);
    }

    public static ArrayList<Double> getWeightChanges(ArrayList<Double> weights) {

        for (int i = 0; i < weights.size() - 1; i++) {
            weights.set(i, (weights.get(i + 1) - weights.get(i)));
        }

        return weights;
    }

}

I have attempted above to traverse all the elements of the array list and print out their respective consecutive differences (Taking index one of the array list and subtracting index zero) however, my for loop seems to traverse all elements printing their respective differences but then adds the last number of my initial array to the end of my new array list with all of the differences. How do I fix this?

ublic class PartA {

public static void main(String args[]) {


    ArrayList<Double> weights = new ArrayList<Double>();


    weights.add(145.0);
    weights.add(146.5);
    weights.add(146.5);
    weights.add(147.0);
    weights.add(146.0);
    weights.add(148.0);
    weights.add(148.5);


    ArrayList<Double> printWeightChanges = getWeightChanges(weights);

NEW ATTEMPT:

}

/**
 * Part a
 */

public static ArrayList<Double> getWeightChanges(ArrayList<Double> weights) {

    ArrayList<Double> weightDifferences = new ArrayList <Double>();

    for (int i = 0; i < weights.size() - 1; i++) {
        weightDifferences.add(i, weights.get(i + 1) - weights.get(i));

    }

    System.out.println(weightDifferences);
    return weightDifferences;


    }

}

This generates all sorts of errors :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 145 out of bounds for length 6 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at PartB.getWeights(PartB.java:37) at PartB.main(PartB.java:25)

My attempt Code output from my attempt


Solution

  • It's because of how you defined your loop limits:

    for (int i = 0; i < weights.size() - 1; i++)
    

    The loop changes one less element, than there is in an array (i < weights.size() - 1).

    I would suggest creating another list for differences, something like this:

    public static List<Double> getWeightChanges(ArrayList<Double> weights) {
    
        List<Double> diffs = new ArrayList<>();
        for (int i = 0; i < weights.size() - 1; i++) {
            diffs.add(weights.get(i + 1) - weights.get(i));
        }
    
        return diffs;
    }