试图在java中为数组添加多个双精度数

时间:2015-11-11 03:41:00

标签: java arrays

我试图将购买列表添加到数组中,然后能够根据数组中的双精度执行一些计算。我无法尝试将购买添加到双阵列 这就是我所拥有的:

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];

   }

在我遇到问题时添加购买方式:

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;
         }


   }

4 个答案:

答案 0 :(得分:1)

问题是pur的类型为double []。因此,您需要创建一个大小为purchase + pur的新数组,并将pur的每个元素复制到购买结束。

请尝试以下代码:

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];
  }
}

答案 1 :(得分:1)

pur是一个数组,double... pur离开时允许您将零个或多个值传递给方法,但是在方法中将其视为数组。

考虑到这一点,您试图将purchases数组中的每个元素分配给相同的值pur(或double[]),这显然不起作用。

相反,您需要获取数组的当前长度,按照purpurchases.length + pur.length)的长度重新调整数组的大小,然后从之前的最后位置开始添加新元素来自pur

也许像......

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];
    }

}

现在,只要您认为这可能是一个好主意,您应该考虑使用某种类型的List,或许类似......

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);
        }

    }
}

有关详细信息,请查看Collections Trail

答案 2 :(得分:1)

使用ArrayList代替数组会更简单,并且可以提高代码性能和质量。要创建一个可以购买的商品

protected ArrayList<Double> purchases = new ArrayList<Double>();

然后您的addPurchases方法可以很容易地简化为:

public void addPurchases(double... pur) {
    for (double purchase : pur) {
        purchases.add(purchase);
    }
}

答案 3 :(得分:0)

您可以这样做:

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];
  }

}

但是,请勿使用数组并手动调整大小。如果您需要将未知数量的项目插入集合中,请使用动态大小的集合,例如java.util.Listjava.util.ArrayList