当前位置: 首页 > 知识库问答 >
问题:

Java-我的数组列表只返回空值

颜新
2023-03-14

我对java相当陌生,我不明白为什么我的ArrayList只返回空值!

我正在开发一个通讯录应用程序,它应该将人作为对象存储,应该存储全名、地址、城市、州、邮政编码和电话号码,但在我运行程序后,所有存储的都是空值。。。

人员类别:

public class Person implements Comparable<Person>{
private String FULLNAME;
private String ADDRESS;
private String CITY;
private String STATE;
private String ZIP;
private String PHONE;

public Person(String FULLNAME, String ADDRESS,  String CITY, String STATE, String ZIP, String PHONE) {

}

//GETTERS
public String getFullName(){
    return FULLNAME;
}
public String getAddress(){
    return ADDRESS;
}
public String getCity(){
    return CITY;
}
public String getState(){
    return STATE;
}
public String getZip(){
    return ZIP;
}
public String getPhone(){
    return PHONE;
}

//SETTERS
public void setFullName(String fullname){
    this.FULLNAME = fullname;
}
public void setAddress(String address){
    this.ADDRESS=address;
}
public void setCity(String city){
    this.CITY=city;;
}
public void setState(String state){
    this.STATE=state;
}
public void setZip(String zip){
    this.ZIP=zip;
}
public void setPhone(String phone){
    this.PHONE=phone;
}

TestAddressBook类:

public class TestAddressBook {

static Scanner sc= new Scanner(System.in);

static String fullname;
static String address;
static String city;
static String state;
static String zip;
static String phone;
static String in;
static char input;
public static void main(String[] args){
    //Declare Variables to store user input in before its sent to the 
    //Persons Class and stored in the Array List
    //Instance of a Person
    Person person = new Person(fullname, address, city, state, zip, phone);
    //Declaring ArrayList
    ArrayList<Person> AddressBook = new ArrayList<Person>();

    //Launch Menu
    do{
        Menu();
        //gathers user input
        in=sc.nextLine();
        input=in.toUpperCase().charAt(0);
        //decides what to do based on user selection
        switch (input){
        case 'A': 
            addPerson(AddressBook, person);
            break;
        case 'D':
            //deletePerson(AddressBook);
            break;
        case 'M':
            //modifyPerson(AddressBook);
            break;
        case 'S':
            //search(AddressBook);
            break;  
        }
    }while(input!='Q');
    System.out.println("Application Closed");
    System.exit(0);
}

public static void Menu(){
    //User Selection
    System.out.println("Address Book Menu");
    System.out.println("\tEnter A to (A)dd a Person");
    System.out.println("\tEnter D to (D)elete a Person");
    System.out.println("\tEnter M to (M)odify a Person");
    System.out.println("\tEnter S to (S)earch Address Book");
    System.out.println("\tEnter Q to (Q)uit");
    System.out.println("Please enter your choice");
}

public static void addPerson(ArrayList<Person> AddressBook, Person person){
    System.out.println("Please input the person's information (one line per field)");
    //User Input
    System.out.println("\nPlease enter person's full name: ");
    fullname=sc.nextLine();
    person.setFullName(fullname);
    System.out.println("\nPlease enter person's street address: ");
    address=sc.nextLine();
    person.setAddress(address);
    System.out.println("\nPlease enter person's city: ");
    city=sc.nextLine();
    person.setCity(city);
    System.out.println("\nPlease enter person's state: ");
    state=sc.nextLine();
    person.setState(state);
    System.out.println("\nPlease enter person's zip code: ");
    zip=sc.nextLine();
    person.setZip(zip);
    System.out.println("\nPlease enter person's phone number: ");
    phone=sc.nextLine();
    person.setPhone(phone);


    //Set arrayList
    AddressBook.add(new Person(fullname, address, city, state, zip, phone));

    //test to see if values stored
    System.out.println(AddressBook.get(0).getFullName());

}

添加一个人后,返回的所有内容都是“Null”

任何帮助都将不胜感激,谢谢

共有3个答案

卫博
2023-03-14

那是因为你的构造函数什么都没做。

你需要这样的东西。

public Person(String fullName, String address,  String city, String state, String zip, String phone) {
    this.fullName = fullName;
    this.address = address;
    ... and so on ...
}

请注意,我已将参数名称更改为camel大小写。这是为了遵守Java命名约定。

曾光誉
2023-03-14

你的构造函数什么都没做。您需要为私有实例变量赋值
例如
这个。全名=全名

龙昊焱
2023-03-14

问题不在于地址簿,而在于人。不要通过一个人,也不要使用setter方法。

然后,您所要做的就是将每个值设置为String(或int),以便在创建一个人时传递它们。

最后,需要在构造函数中设置字段。

 类似资料:
  • 有人能告诉我为什么列表返回空吗?我的xpath是准确的,因为我重新检查了它,但我仍然无法迭代它,而调试for循环甚至没有执行。我不确定我哪里出了问题。

  • 问题内容: 我正在尝试编写一个用于与last.fm API进行交互的小脚本。 我有一点使用的经验,但是以前使用它的方式似乎无效,而是返回一个空列表。 我删除了API密钥,因为我不知道它到底应该有多私密,并举了一个示例,说明了我在该位置接收的XML。 与API交互的类: 调用的get_now_playing方法: 我收到的xml样本: 问题答案: 问题在于, 如果给定标签名称,则仅搜索元素的直接后代

  • 我试图用刮擦和飞溅来刮取衣服的图像和一些产品信息。我想得到的形象,只有产品(所以没有模型)。比如这张照片https://www2.hm.com/nl_nl/productpage.0220094001.html 然而,如果我试图让src在Scrapy shell中 回答xpath('//figure[包含(@class,“secondary”)]///img//@src')。摘录() 返回一个空列

  • 问题内容: 我有两个模型,用户模型和时间表,我想用$ lookup 和猫鼬把这两个模型结合起来。 用户(型号) 时间表(型号) 现在我的查询使用猫鼬: 用户汇总 我的查询结果是一个空的array(),如下所示: 查询结果: 我不知道为什么,但是查询结果是一个 空数组 ,我试图使用$ unwind和$ match,但也无法正常工作。 编辑: 用户集合 时间表的收集 问题答案: 猫鼬在创建时将集合名称

  • 我有一个注释定义如下: 我是这样使用它的: 现在,我有了第二个接口,它扩展了第一个: 我想获得MySecondInterface的所有注释,这意味着我也想获得超级接口上定义的注释。 我所尝试的: ######################################################################################### 结果是: 在所有情况下,

  • 昨天我的Nexus5收到了从到版本的更新。此后,扫描设备中可用网络的动作停止接收列表,在这种情况下,结果列表的大小为0,即使在Wifi系统设置中列出了10+个Wifi网络。 这方面的代码通常是:注册并在接收器中等待事件,如下所示: 我在API的变化主题中搜索了关于这个的内容,但是我没有看到这个功能有什么突破性的变化。 有人注意到了吗?是API中的新内容还是仅有的个别情况?