Reputation: 33
I'm trying to add a list of purchases to an array and then be able to perform some calculations based on the doubles in the array. Im having trouble trying to add purchases to the double array Here's what I have:
public abstract class Customer {
protected String category;
protected String acctNumber;
protected String name;
protected double[] purchases;
protected static final double SALES_TAX_RATE = 0.08;
/**
*Reads in customer data.
*@param acctNumberIn customers account number.
*@param nameIn customers name.
*/
public Customer(String acctNumberIn, String nameIn) {
acctNumber = acctNumberIn;
name = nameIn;
purchases = new double[0];
}
Add purchases method where I'm having problems:
public void addPurchases(double ... pur) {
purchases = Arrays.copyOf(purchases, purchases.length + 1);
int a = purchases.length;
for (int i = 0; i < purchases.length; i++) {
purchases[a] = pur;
}
}
Upvotes: 1
Views: 1045
Reputation: 347184
pur
is an array, double... pur
is away that allows you to pass zero or more values to a method, but which are treated as an array from within the method.
With this in mind, you are attempting to assign every element in your purchases
array to the same value pur
(or a double[]
) which obviously won't work.
Instead, you need to get the current length of the array, re-size the array by the length of pur
(purchases.length + pur.length
), then from the previously last position begin adding in the new elements from pur
Maybe something like...
public void addPurchases(double... pur) {
int start = purchases.length;
purchases = Arrays.copyOf(purchases, purchases.length + pur.length);
for (int i = start; i < purchases.length; i++) {
purchases[i] = pur[i - start];
}
}
Now, any time you think this might be a good idea, you should consider using a List
of some kind instead, maybe something like...
public static class Customer {
protected String category;
protected String acctNumber;
protected String name;
protected List<Double> purchases;
protected static final double SALES_TAX_RATE = 0.08;
/**
* Reads in customer data.
*
* @param acctNumberIn customers account number.
* @param nameIn customers name.
*/
public Customer(String acctNumberIn, String nameIn) {
acctNumber = acctNumberIn;
name = nameIn;
purchases = new ArrayList<>(25);
}
public void addPurchases(double... pur) {
for (double p : pur) {
purchases.add(p);
}
}
}
Have a look at the Collections Trail for more details
Upvotes: 1
Reputation: 108
The problem is that pur is of type double[]. So you will need to create a new array with the size of purchases + pur, and copy each element of pur to the end of purchases.
Please try the following code:
public void addPurchases(double ... pur) {
int purchasesLength = purchases.length;
int combinedLength = pur.length + purchasesLength;
purchases = Arrays.copyOf(purchases, combinedLength);
for (int i = purchasesLength, j = 0; i < combinedLength; i++, j++) {
purchases[i] = pur[j];
}
}
Upvotes: 1
Reputation: 34189
You can simply do:
public void addPurchases(double ... pur) {
int a = purchases.length;
purchases = Arrays.copyOf(purchases, purchases.length + pur.length);
for (int i = 0; i < pur.length; i++) {
purchases[a + i] = pur[i];
}
}
However, DO NOT use arrays and resize it manually. If you need to insert unknown number of items into a collection, use dynamic-size collections like java.util.List
or java.util.ArrayList
.
Upvotes: 0
Reputation: 2479
Using an ArrayList
instead of an array would be much simpler, as well as improve your code performance and quality. To create one to hold your purchases you could do
protected ArrayList<Double> purchases = new ArrayList<Double>();
And then your addPurchases method can easily be simplified to:
public void addPurchases(double... pur) {
for (double purchase : pur) {
purchases.add(purchase);
}
}
Upvotes: 1