简体   繁体   English

关于ArrayList将来使用吗?

[英]About ArrayList in future use?

I am a java beginner and learning the oop concept. 我是Java初学者,正在学习oop概念。 I already success to store the object value into a arraylist and i try to display the arraylist in the main method. 我已经成功地将对象值存储到arraylist中,并且尝试在main方法中显示arraylist。 But the problem is if i remove the add value code in the main method and display again the arraylist. 但是问题是,如果我在main方法中删除了增值代码,然后再次显示arraylist。 The arraylist will show the null value which is []. arraylist将显示为[]的空值。 Please help me and is this is my understanding problem or need to store in txtfile? 请帮助我,这是我的理解问题,还是需要存储在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 数据库或获取或存储arraylist的内容,可用于显示我之前添加的所有记录,更新或删除

This is for my practice project and unuse the database to create a POS system based on oop concept. 这是针对我的实践项目,并且不使用数据库来创建基于oop概念的POS系统。 I had learn php and c# before and i do the same type project and not very confused because of using database. 我以前学过php和c#,并且我做相同类型的项目,并且由于使用数据库而不太困惑。 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. 但是现在我感到困惑如何使用Java创建它,并且能够基于oop概念创建成员,更新成员配置文件等。 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 我希望如果在main中编写代码

 { 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. 如果您不想在主要功能中将客户添加到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 */

}

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

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. 要存储它们,您将必须实现某种存储系统,例如MySQL,或者如果您不必真正从任何地方访问它们,则只需实现一个文本文件。 You will find plenty of tutorials here on Stackoverflow in how to do that. 您将在Stackoverflow上找到许多有关如何执行此操作的教程。

EDIT 编辑

@andy-turner pointed out that doing customers.add(this); @ andy-turner指出这样做是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: 因此,您可以只在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);
}

Variables in memory are ephemeral 内存中的变量是短暂的

An ArrayList , like all of the Java Collections Framework, is a structure for holding data in memory . 像所有Java Collections Framework一样, ArrayList是用于将数据保存在内存中的结构。 When your program ends its execution, all of that memory is freed. 当程序结束执行时,将释放所有该内存。 Your ArrayList is destroyed. 您的ArrayList被破坏。

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 . 在下一次运行时,请读取该文件,将文本解析回对象,然后填充新的ArrayList

You can open a file and have your ArrayList write itself to storage using Java Serialization technology. 您可以打开一个文件,并使用Java序列化技术让ArrayList将自身写入存储。 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 空数组与NULL

The arraylist will show the null value which is []. arraylist将显示为[]的空值。

The string [] represents an empty array, an array holding no elements. 字符串[]表示一个空数组,该数组不包含任何元素。 Such array is not null! 这样的数组不为空! Null means no array at all. Null表示完全没有数组。

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. 那是一个空数组,意味着根本没有数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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