ArrayList中只添加最后一个元素 [英] ArrayList adding only last element

查看:140
本文介绍了ArrayList中只添加最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个for循环code.I发现通过调用自定义显示功能的ABOOK ArrayList对象只增加最后一个类的对象三次。为什么会发生这种情况?

below there is a for loop code.I found out by calling a custom display function that the aBook arrayList object only adding last class object thrice. Why is that happening?

Scanner s = new Scanner(System.in);
    ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>();
    // taking input for every LifIAddressBook and adding them to the ArrayList.
    for (int i = 0; i < 3; i++) {
        System.out.println("Entry " + i+1);
        System.out.print("Please Enter First Name: ");
        String a = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Last Name: ");
        String b = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Street Address: ");
        String c = s.nextLine();
        System.out.println();
        System.out.print("Please Enter City: ");
        String d = s.nextLine();
        System.out.println();
        System.out.print("Please Enter Zip Code: ");
        int e = s.nextInt();
      // in the next line we need to fire a blank scan function in order consume the nextLine. because after executing s.nextInt compiler skip a scan function for a weird reason
        s.nextLine();
        System.out.println();

        LiFiAddressBook x = new LiFiAddressBook(a, b, c, d, e);
        aBook.add(x);


    }

这是我的LiFiAddressBook类

here is my LiFiAddressBook Class

public class LiFiAddressBook {

static  String  first_name, last_name, street_address, city_state;
static int zip_code;

public LiFiAddressBook(String first, String last, String street, String city, int zip) {
  //constructor for class object.
    first_name = first;
    last_name = last;
    street_address = street;
    city_state = city;
    zip_code = zip;
}

public  String get_first() {
    return first_name;
}

public String get_last() {
    return last_name;
}

public String get_address() {
    return street_address;
}

public String get_city() {
    return city_state;
}

public String get_zip() {
    return Integer.toString(zip_code);
}

public static void display() {
    System.out.println("First Name: "+first_name);
    System.out.println("Last Name: "+last_name);
    System.out.println("Street Address"+street_address);
    System.out.println("City State: "+city_state);
    System.out.println("Zip Code: "+zip_code);


}

}

推荐答案

,每次构造结果
公共LiFiAddressBook(字符串,字符串,字符串,字符串,INT)
结果,被称为旧值上写入由新的数据,并使打印在列表中元素的LiFiAddressBook类指向相同的对象的对象的变量。因此,印刷相似的对象。搜索结果
需要明确的是,居然有LiFiAddressBook的3个实例。但变量/那些属性LiFiAddressBook到相同的对象的实例的引用。

Because of the static keyword, every time the constructor
public LiFiAddressBook(String , String , String , String , int )
is called the old values are over written by the new values and when printed the elements in list the variables of the objects of LiFiAddressBook class points to same objects. hence printing similar objects.

To be clear, actually there are 3 instances of LiFiAddressBook . but the variables/properties of those LiFiAddressBook instances references to same objects.

这篇关于ArrayList中只添加最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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