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

查看:36
本文介绍了使用构造函数向 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");

推荐答案

如果要将对象添加到数组列表中,创建你的类类型的数组列表,你可以像这样添加它,

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天全站免登陆