簡體   English   中英

關於ArrayList將來使用嗎?

[英]About ArrayList in future use?

我是Java初學者,正在學習oop概念。 我已經成功地將對象值存儲到arraylist中,並且嘗試在main方法中顯示arraylist。 但是問題是,如果我在main方法中刪除了增值代碼,然后再次顯示arraylist。 arraylist將顯示為[]的空值。 請幫助我,這是我的理解問題,還是需要存儲在txtfile中? 數據庫或獲取或存儲arraylist的內容,可用於顯示我之前添加的所有記錄,更新或刪除

這是針對我的實踐項目,並且不使用數據庫來創建基於oop概念的POS系統。 我以前學過php和c#,並且我做相同類型的項目,並且由於使用數據庫而不太困惑。 但是現在我感到困惑如何使用Java創建它,並且能夠基於oop概念創建成員,更新成員配置文件等。 請幫助我或提出建議。 非常感謝。

我的超級班

class Person {
private List<Customer> customers;
private String name;
private String gender;
private String email;

public Person(){
}

public Person(List<Customer> customers){
    this.customers = customers;
}

public Person(String name, String gender, String email){
    ***
}

public List<Customer> getCustomers(){
    return customers;
}

public void addCustomer(Customer customer){
    customers.add(customer);
}

//Getter
***
//Setter}

我的子類

class Customer extends Person{

private int custID;
private static int customerID = 10001;

public Customer(String name, String gender, String email,int custID){

    super(name, gender, email);
    this.custID = custID;
    customerID++;
}

public int getCustID(){
    return custID;
}

public static int getCustomerID(){
    return Customer.customerID;
}

public String toString(){
    return String.format("%d%30s%7s%30s\n", getCustID(), getName(), getGender(),getEmail());
}

}

我的主要方法

public class POS {

public static void main(String[] args) {

    Customer p1 = new 
Customer("Halo","M","haloworld@gmail.com",Customer.getCustomerID());
    Customer p2 = new 
Customer("Haloo","F","halobitchworld@gmail.com",Customer.getCustomerID()); 


    List<Customer> cList = new ArrayList<>();

    cList.add(p1);  //if remove
    cList.add(p2); // if remove 

    Person customer = new Person(cList);
    System.out.print(customer.getCustomers());

}

}

我希望如果在main中編寫代碼

 { Person person = new Person();
    System.out.print(person);
 }

將顯示我之前添加的結果

如果您不想在主要功能中將客戶添加到ArrayList中,那么一種好方法是在Person類中將List<Customer>設置為static並在創建客戶時將其添加。

public class Person {

    private static List<Person> customers = new ArrayList<>();
    public static List<Person> getCustomers() {
        return customers;
    }

    private String name;
    private String gender;
    private String email;

    public Person(String name, String gender, String email) {
        this.name = name;
        this.gender = gender;
        this.email = email;
        customers.add(this);
    }

    /* getters */

}

現在,在main()函數中,您只需創建Customers ,它們就會自動添加到customers列表中,因此您可以通過調用靜態函數getCustomers()來獲取它們

public static void main(String[] args) {
    Customer p1 = new Customer("Halo","M","haloworld@gmail.com",Customer.getCustomerID());
    Customer p2 = new Customer("Haloo","F","halobitchworld@gmail.com",Customer.getCustomerID());
    System.out.print(Customer.getCustomers());
}

要存儲它們,您將必須實現某種存儲系統,例如MySQL,或者如果您不必真正從任何地方訪問它們,則只需實現一個文本文件。 您將在Stackoverflow上找到許多有關如何執行此操作的教程。

編輯

@ andy-turner指出這樣做是customers.add(this); 在構造函數內部確實很痛苦。 因此,您可以只在Main-class中創建ArrayList<Customer> ,然后像這樣工作:

private static ArrayList<Customer> customers = new ArrayList<>();

public static void main(String[] args) {
    customers.add(new Customer("Halo","M","haloworld@gmail.com",Customer.getCustomerID()));
    customers.add(new Customer("Haloo","F","halobitchworld@gmail.com",Customer.getCustomerID()));
    System.out.print(customers);
}

內存中的變量是短暫的

像所有Java Collections Framework一樣, ArrayList是用於將數據保存在內存中的結構。 當程序結束執行時,將釋放所有該內存。 您的ArrayList被破壞。

存儲

如果要在運行之間共享數據,則必須存儲它。

您可以打開存儲中的文件,並將數據值寫入文本。 在下一次運行時,請讀取該文件,將文本解析回對象,然后填充新的ArrayList

您可以打開一個文件,並使用Java序列化技術讓ArrayList將自身寫入存儲。 或者,您可以使用其他序列化格式自己進行序列化

或將您的數據值發送到數據庫,該數據庫又將它們寫入存儲。 下次運行時,從數據庫檢索。

或者將您的數據通過網絡傳遞給代表您接受數據的某些服務。 在下次運行時,向服務詢問您的數據。

所有這些都太籠統了,無法討論堆棧溢出。 您需要進行自己的研究和學習。

空數組與NULL

arraylist將顯示為[]的空值。

字符串[]表示一個空數組,該數組不包含任何元素。 這樣的數組不為空! Null表示完全沒有數組。

想象一下一個拿着書架的書架。 這就像一個包含元素的數組。 移開書本。 空架子就像一個空數組,沒有元素。 現在,取下書架並將其燃燒。 那是一個空數組,意味着根本沒有數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM