代码之家  ›  专栏  ›  技术社区  ›  user225269

尝试在java中更新数组元素

  •  1
  • user225269  · 技术社区  · 14 年前

    我正在尝试用Java更新一个数组元素。某种类似数据库的程序,但使用数组临时存储数据。

    0 . 如果我试图搜索其他记录,它就找不到了。我不知道为什么。

    boolean blnFound=false;
        String strP=getString("Input product to update: ");
    
        try{
            //loop through the array
            for(int a=0; a<iSl; a++){
                if(strP.compareToIgnoreCase(aProd_name[a])==0){
    
                    //display information match is found
                    Display("Product already registered..");
                    Display("Product ID: ",aProd_id[a]);
                    Display("Product Name: ", aProd_name[a]);
                    Display("Product Description: ", aProd_desc[a]);
                    Display("Product Size: ", aSize[a]);
                    Display("Total Quantity: ", aTotalQty[a]);
    
                    Display("Quantity on hand: ", aQtyonHand[a]);
                    Display("Reorder Quantity: ", aReorder[a]);
                    Display("Dealer Price: ", aDPrice[a]);
                    Display("Selling Price: ", aSPrice[a]);
                    Display("Manufacture date: ", aMDate[a]);
                    Display("Expiry date: ", aEDate[a]);
                    Display("Manufacturer: ", aManufacturer[a]);
                    blnFound=true;
    

    //Input new information
    aProd_id[a]=getInteger("Input new  product id: ");
    
    aProd_desc[a]=getString("Input new product description: ");
    aSize[a]=getString("Input new size: ");
    aTotalQty[a]=getDouble("Input new total quantity: ");
    aQtyonHand[a]=getDouble("Input new quantity on hand: ");
    aReorder[a]=getDouble("Input new reorder: ");
    aDPrice[a]=getDouble("Input new dealer price: ");
    aSPrice[a]=getDouble("Input new selling price: ");
    aMDate[a]=getString("Input new manufactured date: ");
    aEDate[a]=getString("Input new expiration date: ");
    aManufacturer[a]=getString("Input new manufacturer: ");
    Display("Product updated!");
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   jjnguy Julien Chastang    14 年前

    在我看来,您正在使用并行数组来存储一种类型的对象。我可以推荐一个大的改变吗?(不管你说什么我都要说)

    我将创建一个名为 Product

    它看起来像这样:

    public class Product {
    
        // members storing the data
        private int id;
        private String name;
        private String description;
        private String size;
        private int totalQuant;
    
        // the rest go here
    }
    

    然后,我会把它们储存在 Map<Integer, Product> .

    Product someProduct;
    Map<String, Product> dataBase = new HashMap<String, Product>();
    dataBase.put(someProduct.getName(), someProduct);
    

    这也是“更新”数据库的方法。当输入数据库中还没有的内容时,只需创建一个新对象并将其放入 Map .

    然后你可以很容易地搜索 像这样:

    boolean productIsRegistered = dataBase.containsKey(strP);
    
        2
  •  0
  •   aioobe    14 年前

    我真的不知道怎么了。。。实际上,我甚至看不到你在哪里尝试更新数组。

    但是,您可能希望使用 equalsIgnoreCase 而不是 compareToIgnoreCase .

        3
  •  0
  •   Colin Hebert    14 年前

    您可以尝试在以下位置更改for:

    for(int a=0; a<aProd_name.length; a++){
    

        4
  •  0
  •   Riduidel    14 年前

    iSl ? 什么时候定义?各种数组是如何初始化的?这是我给你的建议。

    创建 Product 班级。对于这个类,在这里定义尽可能多的字段(id,name,desc,…)。从你的仓库装载产品,并把它们放在一个仓库里 Collection (一) List ,一个 Set ,以适合您的需求为准)。最后读了一遍。您的代码将更容易理解,因为您必须只迭代一个数组,而不是所有这些数组(我怀疑其中一个数组的大小没有初始化为大于0)。