Java 将ArrayList从一个方法传递到同一类中的另一个方法?

Java 将ArrayList从一个方法传递到同一类中的另一个方法?,java,arraylist,Java,Arraylist,我正在为一家服装店编写一个小项目。 在我的一个类中,将传递2个变量sitem&price 每次用户将选择一个 某些产品 它们将被传递到两种不同的方法中 一个用于添加项目,另一个用于添加价格 我试着取第一个数组,其中包含所有添加到购物车中的衣服,另一个数组包含所有物品的价格 每个项目及其价格都有相似的指数,因为它们是同时添加的 所以现在。。。我的问题是,我确实希望将这两个ArrayList都传递到我的方法PrintRecipt中,这样我就能够打印前面有价格的项目 代码:- package clot

我正在为一家服装店编写一个小项目。 在我的一个类中,将传递2个变量sitem&price 每次用户将选择一个 某些产品

它们将被传递到两种不同的方法中

一个用于添加项目,另一个用于添加价格

我试着取第一个数组,其中包含所有添加到购物车中的衣服,另一个数组包含所有物品的价格

每个项目及其价格都有相似的指数,因为它们是同时添加的

所以现在。。。我的问题是,我确实希望将这两个ArrayList都传递到我的方法PrintRecipt中,这样我就能够打印前面有价格的项目

代码:-

package clothesshop;
import java.util.ArrayList;
public class Cart {
void cartAddi(String item){//adding a certain item will be passed here
    ArrayList<String> clothes = new ArrayList<String>(5);
    clothes.add(item); 
}
int cartAddp(int price){//along with its price to be added to the cart
    ArrayList<Integer> cost = new ArrayList<Integer>(5);
    cost.add(price); int TotalP=0;
    for (int total : cost) {
       TotalP = TotalP  + total; //total price
    }
    return TotalP;
}
void PrintRecipt(){ 
    //taking both ArrayLists and printing them here?
   }  
}

为什么要在函数中声明这两个ArrayList? 将它们作为字段成员,这样您就可以从其他函数访问它们,当然,在您编写的代码中,您不能添加超过1项,因为每次添加都会创建一个新的ArrayList,并且没有以前的cart添加的内存。 将它们设为字段,在add函数中,只需将它们添加到所需特定数组的字段中。
在您询问的函数中,只需对ArrayList大小执行for循环,并在每次迭代中打印第一个数组的第i个元素和第二个数组的第i个元素。

为什么要在其函数中声明这两个ArrayList?
package clothesshop;
import java.util.ArrayList;
public class Cart {
    private ArrayList<String> clothes = new ArrayList<String>(5);
    private ArrayList<Integer> costs = new ArrayList<Integer>(5);
    int cartAdd(String item, int price){//adding a certain item will be passed here
        clothes.add(item); 
        costs.add(price); 
        int TotalP=0;
        for (int total : costs) {
            TotalP = TotalP  + total; //total price
        }
        return TotalP;
    }

    void PrintRecipt() {
        for (int i=0; i<clothes.length; i++) { 
            System.out.println(clothes.get(i) + " at " + costs.get(i));
        }
    }  
}
将它们作为字段成员,这样您就可以从其他函数访问它们,当然,在您编写的代码中,您不能添加超过1项,因为每次添加都会创建一个新的ArrayList,并且没有以前的cart添加的内存。 将它们设为字段,在add函数中,只需将它们添加到所需特定数组的字段中。
在您询问的函数中,只需对ArrayList大小执行for循环,并在每次迭代中打印第一个数组的第i个元素和第二个数组的第i个元素。

您可以使用单个HashMap来存储项目的价格。您需要将其声明为实例变量。然后在PrintRecit中,只需迭代hashmap条目,并根据所需格式打印值

package clothesshop;
import java.util.ArrayList;
public class Cart {
    private ArrayList<String> clothes = new ArrayList<String>(5);
    private ArrayList<Integer> costs = new ArrayList<Integer>(5);
    int cartAdd(String item, int price){//adding a certain item will be passed here
        clothes.add(item); 
        costs.add(price); 
        int TotalP=0;
        for (int total : costs) {
            TotalP = TotalP  + total; //total price
        }
        return TotalP;
    }

    void PrintRecipt() {
        for (int i=0; i<clothes.length; i++) { 
            System.out.println(clothes.get(i) + " at " + costs.get(i));
        }
    }  
}
package clothesshop;
import java.util.ArrayList;
public class Cart {
        private HashMap<String, int> items = new HashMap<String, int>(5);
    int cartAdd(String item, int price){//adding a certain item will be passed here
        items.add(item, price); 
        int TotalP=0;
        for (int total : costs) {
            TotalP = TotalP  + total; //total price
        }
        return TotalP;
    }

    void PrintRecipt() {

    Iterator it = items.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " at " + pair.getValue());

       }

    }  
}

您可以使用一个HashMap来存储商品的价格。您需要将其声明为实例变量。然后在PrintRecit中,只需迭代hashmap条目,并根据所需格式打印值

package clothesshop;
import java.util.ArrayList;
public class Cart {
        private HashMap<String, int> items = new HashMap<String, int>(5);
    int cartAdd(String item, int price){//adding a certain item will be passed here
        items.add(item, price); 
        int TotalP=0;
        for (int total : costs) {
            TotalP = TotalP  + total; //total price
        }
        return TotalP;
    }

    void PrintRecipt() {

    Iterator it = items.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " at " + pair.getValue());

       }

    }  
}

项目1价格1$换行项目2价格2$换行项目3价格3$换行------换行总价:总价$听起来您可以使用一个新类型的集合,将项目描述和成本结合起来。现在也是学习有效的代码格式和Java命名约定的好时机。item 1 price 1$newline item 2 price 2$newline item 3 price 3$newline-----------newline总价:total price$听起来您可以使用一个结合了项目描述和成本的新类型集合。现在也是学习有效的代码格式和Java命名约定的好时机。totalP也应该是类中的一个字段,只需在函数调用时添加到该字段,而不是每次添加一个项时都进行迭代即可,但它只是一个显示问题答案的原型,我不关心Totalptop也应该是类中的一个字段,只需在函数调用时添加到它,而不是每次添加一个项时都进行迭代,但它只是一个显示问题答案的原型,我不关心TotalPDo您认为当用户需要启动多个订单多次购买商品时,他会再次访问您的店铺吗?您认为当用户需要启动多个订单多次购买商品时,他会再次访问您的店铺吗?