简体   繁体   中英

Adding new objects to an ArrayList using a constructor

I have to make a small app as an class exercise which simulates an mobile phone which is able to add and list contacts.

In the main I have to create a new mobile phone by its brand and to add new contacts in it. How can I make an ArrayList which stores the contacts and add them in the List as it follows ?

public static void main(String[] args) {

    Phone phone = new GalaxyS6();//should compile

    phone.addContact("id", "phoneNumber", "firstName", "lastName");

If you want to add the objects to the arraylist, Create the arraylist of your class type and you can add it something like this,

   ArrayList<Phone> phoneList = new ArrayList<>();
   phoneList.add(new phone("id", "phoneNumber", "firstName", "lastName"));

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