I have to write a simple program that declares three arrayLists referenced by the objects named priceList, quantityList, and amountList. Each arrayList should be declared in main() and should be capable of holding a minimum of 10 double-precision numbers.

The numbers that should be stored in price are 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98.

The numbers that should be stored in quantity are 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9 4.8.

The program should pass object references to these three arrays to a method named extend(), which should calculate the elements in the amount array as the product of the corresponding elements in the price and quantity arrays (for example, amount = price * quantity ).

After extend() has put values into the amount array, create a method that displays the results of all three lists. Appropriate formatting techniques need to be used to produce a formatted output.

I have been working on it for awhile but somehow getting a error on the add lines. I have labeled the areas that i am getting the errors. I am sure it is something stupid that is just not popping in my brain right now. Here is my code:

[
import java.util.*;

public class ThreeArrayLists{
public static void main(String[]args){
double [] price={10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
ArrayList priceList=new ArrayList();
for (int i=0; i<price.length; i++) {
priceList.add(price);//////////This is where I am getting the error at add

}
double [] quantity={ 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
ArrayList quantityList=new ArrayList();
for (int i=0; i<quantity.length; i++) {
quantityList.add(quantity);//////////This is where I am getting the error at add
}
double [] amount=new double[10];
ArrayList amountList=new ArrayList();
for(int i=0; i<amount.length; i++){
amount = price*quantity;
amountList.add(amount);//////////This is where I am getting the error at add

}
System.out.println(amountList);
}
}

]

Add type arguments to ArrayList

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

Hope this helps..

import java.util.ArrayList;


public class ThreeArrayLists{
	
	public static void main(String[]args) {
		
		double[] price = new double[] {
				10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
		
		double[] quantity = new double[] {
				4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
		
		ArrayList<Double> priceList = new ArrayList<Double>();
		ArrayList<Double> quantityList = new ArrayList<Double>();
		ArrayList<Double> amountList = new ArrayList<Double>();
		
		for (int i = 0; i < price.length && i < quantity.length; i++) {
			priceList.add(price[i]);
			quantityList.add(quantity[i]);
			amountList.add(price[i] * quantity[i]);	
		}
		
		System.out.println(amountList);
	}
}

Consider this approach. Only 1 loop; much more efficient.

This article has been dead for over six months. Start a new discussion instead.