如何将构造函数中的ArrayList传递给另一个类 [英] how to pass ArrayList in a constructor to another class

查看:102
本文介绍了如何将构造函数中的ArrayList传递给另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java新手并尝试使用ArrayList。从Main方法我想>将ArrayList传递给另一个CustomerAcc类。我需要帮助如何传递> ArrayList,然后如何在类客户代码中获取ArrayList

I am new to java and trying to work on ArrayList. From Main method I want to >pass the ArrayList to another class CustomerAcc. I need help how to pass the >ArrayList and then how to get the ArrayList in the class Customer code here

import java.io.*;
import java.util.*;
import java.lang.Object;

public class CustomerMain {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> custName = new ArrayList<String>();
        ArrayList<String> custZip = new ArrayList<String>();
        double count = 0;
        boolean done = false;
        System.out.print("Please enter Customer name or 'X' to exit: ");

        Scanner in = new Scanner(System.in);

        // Gets user input for name and zip code             
        while (in.hasNext() && !done) {
            String checkInput = in.next();

            if (checkInput.equalsIgnoreCase("X")) {
                done = true;
            }
            else {
                custName.add(checkInput);

                System.out.print("Please enter Zip code: ");
                custZip.add(in.next());
                count++;

                System.out.print("\nPlease enter Customer Name or 'X' to `enter code here`exit: ");
            }
        }
        //Sending the user input data to CustomerAcc class
        CustomerAcc custRecord = new CustomerAcc(custName, custZip, count);
    }
}      

CustomerAcc类:

The CustomerAcc class :

public class CustomerAcc extends CustomerMain {

   public static void CustomerAcc(String custName, String custZip, int count){

   }
}


推荐答案

如果我很清楚你想要访问另一个类中的ArrayList吗?如果在这种情况下,你可以在声明私有ArrayList变量时使用getter和setter技术。

If I understand well that you want to access an ArrayList in another class? if in the case, you can use the getter and setter technique while declaring private ArrayList variables.

private ArrayList<String> custName = new ArrayList<String>();  
private ArrayList<String> custZip = new ArrayList<String>();  



public void setCustName(ArrayList custName){
    this.custName = custName;
}

public ArrayList geCustName(){
    return custName;
}


public void setCustZip(ArrayList custZip){
    this.custZip = custZip;
}

public ArrayList geCustZip(){
    return custZip;
}

这篇关于如何将构造函数中的ArrayList传递给另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆