标签:ret ali col imp enter cto size tin roman
集合框架
需求:将朋友信息定义为一个对象,将对象加入List中并从List中显示
package com.Aha.Best;
public class Friend {
private String name;
private String emial;
private String mobile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmial() {
return emial;
}
public void setEmial(String emial) {
this.emial = emial;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
package com.Aha.Best;
import java.util.*;
public class TestList {
//friendList是一个全局变量,用来存放所有的Friend对象
List friendList = new ArrayList();
public TestList() {
}
public void addFriend(Friend friend){
//调用add方法,往list中添加对象
friendList.add(friend);
}
public int getFriendSize(){
//size是list对象的一个方法
return friendList.size();
}
public void insertFriend(String name,String email,String mobile){
//新建一个Friend对象
Friend friend = new Friend();
friend.setName(name);
friend.setEmial(email);
friend.setMobile(mobile);
this.addFriend(friend);
}
public void showAllFriend(){
System.out.println("name " + "email " + "mobile");
System.out.println("---------------------------------");
Friend friend = new Friend();
//循环显示,list也是从0开始算起
for(int i = 0 ; i < this.getFriendSize(); i++){
friend = (Friend)friendList.get(i);
System.out.print(friend.getName() + " ");
System.out.print(friend.getEmial() + " ");
System.out.println(friend.getMobile() + " ");
}
System.out.println("---------------------------------");
System.out.println("size : " + this.getFriendSize());
}
public static void main(String[] args) {
TestList testList1 = new TestList();
//添加两条记录
testList1.insertFriend("holen","holen@263.net","13910118302");
testList1.insertFriend("frank","frank@263.net","13800008888");
testList1.showAllFriend();
}
}
需求:将一些无序的对象置入一个对象集中。
import java.util.Hashtable;
public class TestHashTable {
Hashtable hashTable = new Hashtable();
String strObjectA = new String("this is ObjectA");
String strObjectB = new String("This is ObjectB");
Integer intObjectC = new Integer(2);
Integer intObjectD = new Integer(2345);
public TestHashTable() {
}
public void addObject(){
hashTable.put("my1",strObjectA);
hashTable.put(intObjectC,strObjectB);
hashTable.put(intObjectD,strObjectA);
hashTable.put("cgcg",intObjectC);
hashTable.put(strObjectA,intObjectC);
}
public void printHashtable(){
System.out.println(hashTable);
}
public static void main(String[] args) {
TestHashTable testHashTable1 = new TestHashTable();
testHashTable1.addObject();
testHashTable1.printHashtable();
}
}
标签:ret ali col imp enter cto size tin roman
原文地址:https://www.cnblogs.com/Aha-Best/p/10884536.html