如何在java中编写带有组的数组?

时间:2013-11-05 05:35:56

标签: java arrays

Noob,

我如何在数组中创建组?

receipt[] re11 = {["Book", 12.49, 1, false, true],["Music CD", 14.99, 1, false, false]};

我知道在javascript中你可以使用这样的括号,但我不记得在java中怎么做了。

编辑: 这是我的其他课程。对不起,这可能会有点帮助:

public class receipt {

private double price;
private Integer quantity;
private String itemName;
private boolean imported;
private boolean taxExemption;

private double basicTaxRate = .10;
private double importDuty = .05; 
private double grandTotal;

public receipt(){

}

public receipt(String newItemName, double newPrice, Integer newQuantity, boolean newImported, boolean newTaxExemption){
    itemName = newItemName;
    price = newPrice;
    quantity = newQuantity;
    imported = newImported;
    taxExemption = newTaxExemption;
}

//Accessors
public double getPrice(){
    return price;
}

public Integer getQuantity(){
    return quantity;
}

public String getItemName(){
    return itemName;
}

public boolean getImported(){
    return imported;
}

public boolean getTaxExemption(){
    return taxExemption;
}


//mutators
public void setPrice(double newPrice){
    //JOptionPane.showInputDialog("What is the price of the item?");
    this.price = newPrice;
}

public void setQuantity(Integer newQuantity){
    this.quantity = newQuantity;
}

public void setItemName(String newItemName){
    this.itemName = newItemName;
}

public void setImported(boolean newImported){
    this.imported = newImported;
}

public void setTaxExemption(boolean newTaxExemption){
    this.taxExemption = newTaxExemption;
}

public double computeTax(){
    if(imported == true){
        return price*(1+(basicTaxRate+importDuty));
    }
    else{
        return price*(1+basicTaxRate);
    }
}

public double computeTotal(){
    if (taxExemption = false){
        return computeTax()*quantity;
    }
    else
        return price*quantity;

}

public void computeGrandTotal(){
    grandTotal =+ computeTotal();
    //return grandTotal;        
}

5 个答案:

答案 0 :(得分:1)

这是你的意思吗?

Receipt[] re11 = {new Receipt("Book", 12.49, 1, false, true),new Receipt("Music CD", 14.99, 1, false, false)};

答案 1 :(得分:1)

据我所知,在Java中,你不能原生那样做。 如果“收据”是您自己的类,则必须创建该实例。 假设它有一个构造函数接受String,double和int,以及两个布尔值,它们看起来像这样:

receipt[] re11 = {new receipt("Book", 12.49, 1, false, true),new receipt("Music CD", 14.99, 1, false, false)};

答案 2 :(得分:1)

小组我认为你的意思是一个班级。

class Receipt {
 String item;
 Double price;
 int quantity;
 boolean a; //unsure what the true and false are in your context
 boolean b; //unsure what the true and false are in your context
}

然后你可以像这样使用它

Receipt[] rel1 = {new Receipt("Book", 12.49, 1, false, true),new Receipt("Music CD", 14.99, 1, false, false)};

答案 3 :(得分:0)

首先,Java中的数组与JavaScript中的数组不同。 JavaScript中的数组根据需要扩展,但在Java中则不然。此外,您无法从Java数组中“删除”元素。

您需要的是ArrayList<YourClass>,它是一个可扩展的数组。它还允许您删除所需的元素。

进入分组数据的主题。您将需要自己的类,并定义所需的变量和方法。然后将该类的对象放入ArrayList

所以你会上课:

public class Receipt {
 String item;
 Double price;
 int quantity;
 boolean available; 
 boolean iDoNotKnowWhy; 
 //getters and setters
}  

然后您将有一个ArrayList<Receipt>,您可以在其中添加所需的所有数据。

ArrayList<Receipt> receipts = new ArrayList<>();
receipts.add(new Receipt(....)); // add a new receipt
receipts.add(new Receipts(....)); // another receipt

答案 4 :(得分:0)

就这样 -

import java.util.ArrayList;
/**
 * 
 * @author manish
 *
 */

public class CustomArrayList {

    public static void main(String args[]){
        ArrayList<reciept> custReciept=new ArrayList<reciept>();
        custReciept.add(new reciept("Book", 12.55, true, false));
        custReciept.add(new reciept("Book2", 15.55, true, true));

        for(int i=0;i<custReciept.size();i++){
    System.out.println( custReciept.get(i).name);
    System.out.println( custReciept.get(i).price);
    System.out.println( custReciept.get(i).first);

    }

    }


}
class reciept{
    String name;
    double price;
    boolean first,second;

    public reciept(String name, double price, boolean first, boolean second) {
        super();
        this.name = name;
        this.price = price;
        this.first = first;
        this.second = second;
    }

}

复制粘贴代码并享受..