程序員自學之旅(二)容器

 

java容器:
有兩個接口容器Collection、Map連個接口,Collection下有Set和List連個接口。
Set中的數據對象沒有順序且不重複,List中的數據對象有順序器不可以重複。他們存放的是按單個數據存放的。Map存放是鍵值對key和value。
Set下有HashSet類,List接口有LinkedList和ArrayList兩個類,map下有HashMap類。
(一)容器類簡單使用事例代碼:
import java.util.*;
public class TestCollection {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Collection c=new ArrayList();
  c.add("hello");
  c.add(new Name("f1","l1"));
  c.add(new Integer(100));
  System.out.println(c.size());
  System.out.println(c);
  c.remove(new Name("f1","l1"));
  System.out.println(c);
 }
}

class Name
{
 private String firstName,lastName;
 public Name(String firstName,String lastName)
 {
  this.firstName=firstName;
  this.lastName=lastName;
 }
 public String getFirstName()
 {
  return firstName;
 }
 public String getLastName()
 {
  return lastName;
 }
 public String fullName()
 {
  return firstName+" "+lastName;
 }
 public boolean equals(Object obj)
 {
  if (obj instanceof Name)
  {
   Name name=(Name)obj;
   return (firstName.equals(name.firstName)&&lastName.equals(name.lastName));
  }
  return super.equals(obj);
 }
 public int hashCode()
 {
  return firstName.hashCode();
 }
}
注意要想刪除在即寫的的類必須在類中重寫equals和hashCode。
(二)Iterator的使用:
Colletion接口的容器類都有一個Iterator方法用以返回一個實現了Iterator接口的對象。
Iterator對象稱作爲迭代器,用以方便的實現對容器元素的遍歷操作。
代碼事例:
import java.util.*;
public class TestIterator {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Collection c=new HashSet();
  c.add(new Name("f1","l1"));
  c.add(new Name("f2","l2"));
  c.add(new Name("f3","l3"));
  c.add(new Name("f4jhkhk","l4jkklj"));
  Iterator i=c.iterator();
  while(i.hasNext())
  {
   Name n=(Name)i.next();
   System.out.println(n.getFirstName());
  }
  for(Iterator ii=c.iterator();ii.hasNext();)
  {
   Name n=(Name)ii.next();
   if(n.getFirstName().length()<3)
   {
    ii.remove();
    c.remove(n);
   }
  }
  System.out.println();
  Iterator i2=c.iterator();
  while(i2.hasNext())
  {
   Name n=(Name)i2.next();
   System.out.println(n.getFirstName());
  }
 }

}
(三)for增強:
public class StrongFor {
 public static void main(String[] args) {
  int[] arr={1,2,3,4,5,6,7,8,9,0};
  for(int i:arr)
  {
   System.out.println(i);
  }
 }
}
(四)Set容器事例代碼:
public class TestSet {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Set s=new HashSet();
  s.add(new Name("a","a"));
  s.add(new Name("b","b"));
  s.add(new Name("c","c"));
  s.add(new Name("a","a"));//相同元素不會添加
  s.add(new Name("e","e"));
  s.add(new Name("b","b"));//相同元素不會添加
  Iterator i=s.iterator();
  while(i.hasNext())
  {
   Name n=(Name)i.next();
   System.out.println(n.fullName());
  }
 }
}
事例代碼二:
import java.util.*;
public class TestSet2 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Set s1 = new HashSet();
  Set s2 = new HashSet();
  s1.add("a");
  s1.add("b");
  s1.add("c");
  s2.add("d");
  s2.add("a");
  s2.add("b");
  Set sn = new HashSet(s1);
  sn.retainAll(s2);//與s2的交集
  System.out.println(sn);
  Set su = new HashSet();
  su.add(s2);//重複的不添加
  System.out.println(su);
 }
}
(五)List容器事例:
事例代碼一:
import java.util.*;
public class TestList {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List l1=new LinkedList();
  for(int i=0;i<6;i++)
  {
   l1.add("a"+i);
  }
  System.out.println(l1);
  l1.add(3,"sdasdas");
  System.out.println(l1);
  l1.set(6, "kpiiioio");
  System.out.println(l1);
  System.out.println(l1.get(4));
  System.out.println(l1.indexOf("a4"));
  l1.remove(1);
  System.out.println(l1);
 }
}
事例代碼二:
import java.util.*;
public class TestList2 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List l1=new LinkedList();
  List l2=new LinkedList();
  for(int i=0;i<6;i++)
  {
   l1.add("a"+i);
  }
  System.out.println(l1);
  Collections.shuffle(l1);//隨機排列
  System.out.println(l1);
  Collections.reverse(l1);//逆序排列
  System.out.println(l1);
  Collections.sort(l1);//排序
  System.out.println(l1);
  System.out.println(Collections.binarySearch(l1, "a5"));
 }
}
(六)Comparable 接口(兩個類之間比較大小)
所有可以“排序”的類都可以實現了java.lang.Comparable接口,java.lang.Comparable接口中只有一個方法public int comlareTo(Object obj);
事例代碼:
class Name implements Comparable
{
 @Override
 public int compareTo(Object arg0) {
  // TODO Auto-generated method stub
  Name n=(Name)arg0;
  int lastCmp=lastName.compareTo(n.lastName);
  return (lastCmp!=0?lastCmp:firstName.compareTo(n.firstName));
 }
 private String firstName,lastName;
 public Name(String firstName,String lastName)
 {
  this.firstName=firstName;
  this.lastName=lastName;
 }
 public String getFirstName()
 {
  return firstName;
 }
 public String getLastName()
 {
  return lastName;
 }
 public String fullName()
 {
  return firstName+" "+lastName;
 }
 public boolean equals(Object obj)
 {
  if (obj instanceof Name)
  {
   Name name=(Name)obj;
   return (firstName.equals(name.firstName)&&lastName.equals(name.lastName));
  }
  return super.equals(obj);
 }
 public int hashCode()
 {
  return firstName.hashCode();
 }
}
(七)如何選擇詩句結構
Array讀快改慢
Linked改快讀慢
Hash兩者之間
(八)Map接口
實現Map接口的類用來存儲鍵-值對
Map接口的實現類有HashMap和TreeMap等。
Map類中存儲的鍵值對通過鍵-值來標識,所以鍵值不能重複。
事例代碼:
import java.util.*;
public class TestMap {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Map m1=new HashMap();
  Map m2=new TreeMap();
  m1.put("one", new Integer(1));
  m1.put("two", new Integer(2));
  m1.put("three", new Integer(3));
  m2.put("A", new Integer(1));
  m2.put("B", new Integer(2));
  System.out.println(m1.size());
  System.out.println(m1.containsKey("one"));
  System.out.println(m2.containsValue(new Integer(1)));
  if(m1.containsKey("two"))
  {
   int i=((Integer)m1.get("two")).intValue();
   System.out.println(i);
  }
  Map m3=new HashMap(m1);
  m3.putAll(m2);
  System.out.println(m3);
 }
}
(九)自動打包、解包
打包:自動將基礎的類型轉換爲對象
解包:自動將對象轉換爲基礎類型
代碼事例:
import java.util.*;
public class TestBox {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Map m1=new HashMap();
  Map m2=new TreeMap();
  //打包
  //m1.put("one", new Integer(1));
  m1.put("one", 1);
  //m1.put("two", new Integer(2));
  m1.put("two", 2);
  //m1.put("three", new Integer(3));
  m1.put("three", 3);
  //m2.put("A", new Integer(1));
  m2.put("A", 1);
 // m2.put("B", new Integer(2));
  m2.put("B", 2);
  //m2.put("B", 3434);
  System.out.println(m1.size());
  System.out.println(m1.containsKey("one"));
 // System.out.println(m2.containsValue(new Integer(1)));
  System.out.println(m2.containsValue(1));
  if(m1.containsKey("two"))
  {
   //解包
   //int i=((Integer)m1.get("two")).intValue();
   int i=(Integer)m1.get("two");
   System.out.println(i);
  }
  Map m3=new HashMap(m1);
  m3.putAll(m2);
  System.out.println(m3);
 }
}
(十)泛型
代碼事例:
import java.util.*;
public class Test {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  List<String> l1=new ArrayList<String>();
  l1.add("sd");
  l1.add("dfsdty");
  l1.add("fsdfsd");
  l1.add("rgfgdfhgdf");
  System.out.println(l1);
  for(int i=0;i<l1.size();i++)
  {
   System.out.println(l1.get(i));
  }
  System.out.println();
  Collection<String> c=new HashSet<String>();
  c.add("sds");
  c.add("dfd");
  c.add("bn");
  c.add("piu");
  c.add("dfsd");
  for(Iterator<String> i=c.iterator();i.hasNext();)
  {
   System.out.println(i.next());
  }
 }
}
class MyName implements Comparable<MyName>
{
 int age;

 @Override
 public int compareTo(MyName mn) {
  // TODO Auto-generated method stub
  if(this.age>mn.age)
   return 1;
  else
   if(this.age<age)
    return -1;
   else
    return 0;
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章