23集合框架

集合框架

12.集合框架

12.1接口

12.1.1 Collection

12.2列表

12.2.1List

需求:将朋友信息定义为一个对象,将对象加入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","[email protected]","13910118302");

testList1.insertFriend("frank","[email protected]","13800008888");

testList1.showAllFriend();

}

}

12.2.2Vector

12.2.3ArrayList

12.2.4LinkedList

12.3集合

12.3.1Set

12.3.2SortedSet

12.3.3TreeSet

12.3.4HashSet

需求:将一些无序的对象置入一个对象集中。

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

}

}

12.4映射

12.4.1Map

12.4.2HashMap

12.4.3TreeMap

猜你喜欢

转载自www.cnblogs.com/Aha-Best/p/10884536.html