我写的关于容器的一个小程序

package execise;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator; 
import java.util.Map;
import java.util.Set;
public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int l = 0;
  List l1 = new ArrayList();
  PhoneNumber pn = new PhoneNumber("021", "852369", "789");
  l1.add(pn);
  l1.add(new PhoneNumber("021", "456123", "123"));
  l1.add(new PhoneNumber("021", "456473", "145"));
  l1.add(new PhoneNumber("021", "456153", "323"));
  l1.add(new PhoneNumber("021", "456120", "923"));
  for (Iterator it = l1.iterator(); it.hasNext(); ){
   System.out.println(l1.get(l));
   it.next();
   l++;
  }
  l1.add(5,pn);
  System.out.println(l1.contains(pn));
  System.out.println(l1.lastIndexOf(pn));
  
  Map<String, PhoneNumber> m = new HashMap<String, PhoneNumber>();
  m.put("a", new PhoneNumber("021", "555555", "555"));
  m.put("b", new PhoneNumber("021", "111111", "111"));
  m.put("c", new PhoneNumber("021", "999999", "999"));
  Set s = m.keySet();
  Collection c = m.values();
  Set s1 = m.entrySet();
  Set s2 = new HashSet();
  s2.add(new PhoneNumber("021", "555555", "555"));
  s2.add(new PhoneNumber("021", "111111", "111"));
  s2.add(new PhoneNumber("021", "999999", "999"));
  for (Iterator it = s2.iterator(); it.hasNext();){
   System.out.println(it.next());
  }
  
  System.out.println(s);
  System.out.println(c);
  for (Iterator it = s1.iterator(); it.hasNext();){
   Map.Entry me = (Map.Entry)it.next();
   System.out.println(me.getKey() + "--" + me.getValue());
  }
  for (Iterator it = s1.iterator(); it.hasNext();){
   Map.Entry me = (Map.Entry)it.next();
   me.setValue("aa" + it);
   System.out.println(me.getKey() + "---" + me.getValue());
   //System.out.println(me.getKey() + "--" + me.getValue());
  }
  for (Object sp : l1) {
   System.out.println(sp);
  }
 }
}
class PhoneNumber implements Cloneable{
 private String quhao;
 private String dianhuahaoma;
 private String kuozhan;
 public String getQuhao() {
  return quhao;
 }

 public void setQuhao(String quhao) {
  this.quhao = quhao;
 }

 public String getDianhuahaoma() {
  return dianhuahaoma;
 }

 public void setDianhuahaoma(String dianhuahaoma) {
  this.dianhuahaoma = dianhuahaoma;
 }

 public String getKuozhan() {
  return kuozhan;
 }

 public void setKuozhan(String kuozhan) {
  this.kuozhan = kuozhan;
 }

 public PhoneNumber(String dianhuahaoma, String kuozhan, String quhao){
  this.dianhuahaoma = dianhuahaoma;
  this.kuozhan = kuozhan;
  this.quhao = quhao;
 }
 public PhoneNumber(){
  
 }
 public boolean equals (Object obj) {
  if (null == obj) {
   return false;
  }
  if (this == obj) {
   return true;
  }
  if (!(obj instanceof PhoneNumber)) {
   return false;
  }
  PhoneNumber temp = (PhoneNumber) obj;
  if ((this.dianhuahaoma.equals(temp.dianhuahaoma)) && (this.kuozhan.equals(temp.kuozhan)) && (this.quhao.equals(temp.quhao))) {
   return true;
  }
  else{
   return false;
  }
 }
 protected Object clone() throws CloneNotSupportedException{
  super.clone();
  PhoneNumber result = new PhoneNumber();
  result.setDianhuahaoma(this.dianhuahaoma);
  result.setKuozhan(this.kuozhan);
  result.setQuhao(this.quhao);
  return result;
 }
 public String toString(){
  return this.getClass().getName() + '@' + Integer.toHexString(hashCode());
 }
 public int hashcode(){
  int result = 17;
  result = result * 37 + quhao.length();
  result = result * 37 + dianhuahaoma.length();
  result = result * 37 + kuozhan.length();
  return result;
 }
 protected void finalize(){
  
 }
 public void setContent(String dianhuahaoma, String kuozhan, String quhao) {
  this.dianhuahaoma = dianhuahaoma;
  this.kuozhan = kuozhan;
  this.quhao = quhao;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值