使用构造函数将新对象添加到ArrayList [英] Adding new objects to an ArrayList using a constructor

查看:212
本文介绍了使用构造函数将新对象添加到ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做一个小应用程序作为课堂练习,模拟一个能够添加和列出联系人的手机。

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

总的来说,我必须以其品牌创建新的手机并在其中添加新的联系人。我如何制作一个存储联系人的ArrayList并将其添加到列表中,如下所示?

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


推荐答案

如果要将对象添加到arraylist,
创建您的类类型的arraylist,然后可以添加类似的内容,

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

这篇关于使用构造函数将新对象添加到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆