簡體   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();
    }
}

我想在調用函數菜單上的函數DisplayTotalCost時傳遞ArrayList<person>客戶。 我怎么做? 我將如何將addcustomer.totalamount = addcustomer.totalamount + additem.total放在addcustomer.totalamount = addcustomer.totalamount + additem.total來總結所有的additem.total

班級

public class person {

        String name;
        String Address;

            String contactnumber;
        Double totalamount;
}
public class items {

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

}

您可以嘗試此操作並在必要時進行修改:)

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> 需要將 Arraylist 從 java 程序傳遞給 R 腳本作為參數 Java ArrayList <Double> 作為參數 Arraylist和參數Java Java - ArrayList <Integer>作為參數......? 在Java中將ArrayList作為泛型參數傳遞 Java-ArrayList <E> 功能參數 發送 Java arraylist 作為 JavaScript 參數 如何將一類的arraylist對象傳遞給以對象為參數的通用方法 Java遞歸通過ArrayList中的引用
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM