简体   繁体   中英

About ArrayList in future use?

I am a java beginner and learning the oop concept. I already success to store the object value into a arraylist and i try to display the arraylist in the main method. But the problem is if i remove the add value code in the main method and display again the arraylist. The arraylist will show the null value which is []. Please help me and is this is my understanding problem or need to store in txtfile? database or what to get the or store the arraylist and can use for display all the record, update or delete that i add before

This is for my practice project and unuse the database to create a POS system based on oop concept. I had learn php and c# before and i do the same type project and not very confused because of using database. But now i feel confused how to use the java to create it and can has ability to create member, update member profile and etc based on oop concept. Please help me or give the suggestion. Very thank you.

my super class

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}

my subclass

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

}

My main method

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());

}

}

i expect if write the code in main like

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

will display the result that i add before

If you don't want to add the customers to an ArrayList in your main-function a good way to do it would be to set the List<Customer> static in your Person -class and adding the customers as they get created.

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 */

}

now in your main()-function you only have to create the Customers and they automatically get added to the customers list and therefore you can then get them by calling the static function 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());
}

To store them you would have to implement some kind of storage system like MySQL or simply a text file if you don't really have to access them from everywhere. You will find plenty of tutorials here on Stackoverflow in how to do that.

EDIT

@andy-turner pointed out that doing customers.add(this); inside a constructor really is a pain. So you could just create the ArrayList<Customer> in your Main-class and then work like this:

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);
}

Variables in memory are ephemeral

An ArrayList , like all of the Java Collections Framework, is a structure for holding data in memory . When your program ends its execution, all of that memory is freed. Your ArrayList is destroyed.

Storage

If you want to share data between runs, you must store it.

You can open a file in storage and write data values as text. On next run, read that file, parse the text back into objects, and populate a new ArrayList .

You can open a file and have your ArrayList write itself to storage using Java Serialization technology. Or you can do the serialization yourself with another serialization format .

Or send your data values to a database, which in turn writes them to storage. On next run, retrieve from database.

Or pass your data over the network to some service which accepts the data on your behalf. On next run, ask the service for your data.

All of this is too broad to discuss on Stack Overflow. You need to do your own research and learning.

Empty array versus NULL

The arraylist will show the null value which is [].

The string [] represents an empty array, an array holding no elements. Such array is not null! Null means no array at all.

Imagine a bookshelf holding books. That's like an array holding elements. Remove the books. The empty shelf is like an empty array, with no elements. Now take down the bookshelf and burn it. That's a null array, meaning no array at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM