简体   繁体   English

在 Java 中将 ArrayList 作为参数传递

[英]Pass ArrayList as parameter in Java

import java.util.ArrayList;
import java.util.Scanner;

public class inventory {

        ArrayList<person> customerlist = new ArrayList<person>();
        ArrayList<items> itemlist = new ArrayList<items>(); 

    public void menu (){
        Scanner in = new Scanner(System.in);
            try
                {
                    System.out.println("[1] Add Customer");
                    System.out.println("[2] Show Total Cost ");
                    System.out.println("[3] exit");
                    System.out.print("Choose one : ");
                    int num = in.nextInt();

                    switch(num){
                        case 1 : AddCustomer();//add customer
                            break;
                        case 2 : DisplayTotalCost(customers);//display total cost
                            break;
                        case 3 : System.exit(0);
                        default : break;
                        }   
                }
            catch(Exception e)
                {
                    System.out.println("error: " + e);
                }   
        }

    public void AddCustomer(){
        inventory function = new inventory();
        Scanner in = new Scanner(System.in);
        person addcustomer = new person();
        items additem = new items();

        System.out.println("Enter Customer Name:");
        try {
                addcustomer.name = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Customer Address:");
        try {
                addcustomer.Address = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Customer Contact Number:");
        try {
                addcustomer.contactnumber = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            }

        System.out.println("Do you want to add an item? [1]YES [2]NO");
        int x = in.nextInt();

        while (x == 1){
                function.addItem();

                System.out.println("Do you want to add an item? [1]YES [2]NO");
                x = in.nextInt();

        }

                System.out.println(addcustomer.totalamount);
                function.menu();
        }

    public void addItem(){
        Scanner in = new Scanner(System.in);
        person addcustomer = new person();
        items additem = new items();
        addcustomer.totalamount = .0;

        System.out.println("Enter Item Name:");
        try {
                additem.itemname = in.nextLine();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Item Quantity:");
        try {
                additem.quantity = in.nextInt();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 
        System.out.println("Enter Item Price:");
        try {
                additem.price = in.nextDouble();
            } 
        catch (Exception e) 
            {
                e.printStackTrace();
            } 

        additem.total = (additem.quantity*additem.price);
        System.out.println("Total Cost:" + additem.total);

    }

    public void DisplayTotalCost(ArrayList<person> customers){
        items item = new items();
        person customer = new person();

        System.out.println("Name" + "\t" + "Total Cost");
        for (int i=0; i<customerlist.size(); i++){
            System.out.println(customer.name + "\t");
        }

    }        

    public static void main(String[] args){
        inventory function = new inventory();
        function.menu();
    }
}

I want to pass the ArrayList<person> customers when calling the function DisplayTotalCost on the function menu.我想在调用函数菜单上的函数DisplayTotalCost时传递ArrayList<person>客户。 How do I do that?我怎么做? And how where will I put addcustomer.totalamount = addcustomer.totalamount + additem.total to sum all the additem.total ?我将如何将addcustomer.totalamount = addcustomer.totalamount + additem.total放在addcustomer.totalamount = addcustomer.totalamount + additem.total来总结所有的additem.total

classes班级

public class person {

        String name;
        String Address;

            String contactnumber;
        Double totalamount;
}
public class items {

    String itemname;
    int quantity;
    Double price;
    Double total;

}

You may try this and modify where necessary :)您可以尝试此操作并在必要时进行修改:)

import java.util.ArrayList;
import java.util.Scanner;

public class inventory {
static ArrayList<person> customerlist = new ArrayList<person>();
static ArrayList<items> itemlist = new ArrayList<items>(); 

public static void menu (){      
    Scanner in = new Scanner(System.in);
    try{
        System.out.println("[1] Add Customer");
        System.out.println("[2] Show Total Cost ");
        System.out.println("[3] exit");
        System.out.print("Choose one : ");
        int num = in.nextInt();
        switch(num){
        case 1 : AddCustomer();//add customer
            break;
        case 2 : DisplayTotalCost();//display total cost
            break;
        case 3 : System.exit(0);
            default : break;
        }   
    }
    catch(Exception e){
        System.out.println("error: " + e);
    }   
}

public static void AddCustomer(){
    Scanner in = new Scanner(System.in);
    person addcustomer = new person();
    items additem = new items();
    try {
        System.out.println("Enter Customer Name:");
        addcustomer.name = in.nextLine();
        System.out.println("Enter Customer Address:");
        addcustomer.Address = in.nextLine();
        System.out.println("Enter Customer Contact Number:");
        addcustomer.contactnumber = in.nextLine();
        customerlist.add(addcustomer);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    addItem();
    menu();
}

public static void addItem(){
    Scanner in = new Scanner(System.in);
    person addcustomer = new person();
    items additem = new items();
    addcustomer.totalamount = .0;      
    try {
        System.out.println("Enter Item Name:");
        additem.itemname = in.nextLine();
        System.out.println("Enter Item Quantity:");
        additem.quantity = in.nextInt();
        System.out.println("Enter Item Price:");
        additem.price = in.nextDouble();
        itemlist.add(additem);
    } 
    catch (Exception e) {
        e.printStackTrace();
    } 
    additem.total = (additem.quantity*additem.price);
    System.out.println("Total Cost:" + additem.total);
}

public static void DisplayTotalCost(){
    System.out.println("Name" + "\t" + "Total Cost");
    for (int i=0; i<customerlist.size(); i++){
        System.out.println(customerlist.get(i).name + "\t"+itemlist.get(i).total);
    }
}        

public static void main(String[] args){
    menu();
}
}

传安 ArrayList<customobject> 到接受 ArrayList 作为参数的 function<object> 在 Java<div id="text_translate"><p> 我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。</p><p> 这是我的 function:</p><pre> public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) { this.m_ArrData = arrData; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }</pre><p> 当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,</p><pre> m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter( (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));</pre><p> 处理这种情况的最佳方法是什么,谢谢</p></div></object></customobject> - Pass An ArrayList<CustomObject> to a function that accepts as parameter an ArrayList<Object> in Java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传安 ArrayList<customobject> 到接受 ArrayList 作为参数的 function<object> 在 Java<div id="text_translate"><p> 我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。</p><p> 这是我的 function:</p><pre> public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) { this.m_ArrData = arrData; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }</pre><p> 当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,</p><pre> m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter( (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));</pre><p> 处理这种情况的最佳方法是什么,谢谢</p></div></object></customobject> - Pass An ArrayList<CustomObject> to a function that accepts as parameter an ArrayList<Object> in Java 需要将 Arraylist 从 java 程序传递给 R 脚本作为参数 - Need to pass Arraylist from java program to R script as a parameter Java ArrayList <Double> 作为参数 - Java ArrayList<Double> as Parameter Arraylist和参数Java - Arraylist and parameter Java Java - ArrayList <Integer>作为参数......? - Java - ArrayList<Integer> as Parameter…? 在Java中将ArrayList作为泛型参数传递 - Passing ArrayList as generics parameter in Java Java-ArrayList <E> 功能参数 - Java - ArrayList<E> Parameter in function 发送 Java arraylist 作为 JavaScript 参数 - Send Java arraylist as JavaScript parameter 如何将一类的arraylist对象传递给以对象为参数的通用方法 - how to pass a arraylist object of one class to generic method which is having object as its parameter in java Java递归通过ArrayList中的引用 - Java Recursion Pass by Reference in ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM