Adding new objects to an ArrayList using a constructor

瘦欲@ 提交于 2019-12-11 17:16:03

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/53381965/adding-new-objects-to-an-arraylist-using-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!