集合中存储自定义对象源代码
生活随笔
收集整理的這篇文章主要介紹了
集合中存储自定义对象源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
集合中存儲自定義對象:
package attention;import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;public class Main_7 {public static void main(String[] args){Collection coll=new ArrayList();coll.add(new Person("List1",21));coll.add(new Person("List2",22));coll.add(new Person("List3",23));// 取出元素Iterator it=coll.iterator();while(it.hasNext()){Person p=(Person)it.next();System.out.println(p.getName()+":"+p.getAge());}} } package attention;public class Person {private String name;private int age;// alt+shift+s 獲得get、set方法public Person() {super();// TODO Auto-generated constructor stub}public Person(String name, int age) {super();this.name = name;this.age = age;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}/*** @return the age*/public int getAge() {return age;}/*** @param age the age to set*/public void setAge(int age) {this.age = age;}}總結
以上是生活随笔為你收集整理的集合中存储自定义对象源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Collection中的基本方法
- 下一篇: 阿里技术专家都铎:一文搞懂技术债