I have a method that takes in a string array and finds the averages of every few items depending on the length. I would like the method to delete the first few items in the array depending on the value of offset.
public static double[] getMovingAverage(String[] priceList, int length, int offset){
double[] newPriceList = convert(priceList);
int listLength = newPriceList.length;
int counter = 0;
double listsum;
double[] movingAverage = new double[listLength];
try{
for (int aa = 0; aa < listLength-1; aa++){
listsum = 0;
for (int bb = 0; bb < length; bb++){
counter = aa+bb;
listsum = listsum + newPriceList[counter];
}
movingAverage[aa] = listsum / length;
}
if (offset>0){
//remove first #offset# elements
}
}catch(Exception e){
System.out.println(e);
}
return movingAverage;
}
*note: convert(); converts String[] to double[]
Arrays are fixed-length in Java. (You can’t change the length of an array.)
You can however create a new array with the first
offsetelements removed quite easily:Output: