A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 Himoriarty 于 2015-6-10 13:05 编辑

------- <a  target="blank">android培训</a>、<a  target="blank">java培训</a>、期待与您交流! ----------


增加元素:

      1.1 ArrayList和LinkedList元素为有序的,即存入和取出 的顺序一致。元素可以重复,即在使用add()方法添加元素时,不进行判断,返回值按照 Collection.add(E) 的指定,直接添加,且将指定的元素添加到此列表的尾部。
      如果要去掉相同元素必须要在自定义去除相同元素的方法,在方法中将使用到contains(),此方法要使用到equals()方法,所以在对象中覆盖Object类中的equals()方法。
      1.2 HashSet和TreeSet元素为无序的,即存入和取出的顺序不一致。元素不可以重复,即在使用add()方法添加元素时,需要进行判断,如果此 set 中尚未包含指定元素,则添加指定元素。更确切地讲,如果此 set 没有包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则向此 set 添加指定的元素 e。如果此 set 已包含该元素,则该调用不更改 set 并返回 false。
         对于增加字符串或者数字HashSet、TreeSet这四种都可以经过判断之后存储。
         对于添加对象,HashSe可以直接存入,因为对于HashSet来说,比较的是地址值,每个新建对象即使属性值相同,地址值也不同,所以会默认为是不同元素存入,若要去除属性值相同的元素必须要自定义方法去除,为提高效率对哈希值依据属性值进行修改,这样如果属性值不相同则哈希值也不相同可以直接判断,若哈希值相同,不进行存储,不过为了更加严谨,需要覆盖equals(),对属性值进行比较。但TreeSet不行,因为它要对元素进行比较之后按照顺序存储,而对对象来说,基于哪个属性进行排序方法内部不能自行判断,且没有默认方法,必须要继承Comparable接口,实现compareTo()方法,否则会抛出ClassCastException -(如果指定对象无法与此 set 的当前元素进行比较)。

  1. <div class="blockcode"><blockquote>/**
  2. LinkedList编写栈
  3. */
  4. import java.util.*;
  5. class Duilie
  6. {
  7.         private LinkedList link;
  8.         Duilie()
  9.         {
  10.                 link = new LinkedList();
  11.         }
  12.        
  13.         public void myAdd(Object obj)
  14.         {
  15.                 link.addFirst(obj);
  16.         }
  17.        
  18.         public Object myGet()
  19.         {
  20.                 return link.pollLast();
  21.         }
  22.        
  23.         public boolean isNull()
  24.         {
  25.                 return link.isEmpty();
  26.         }
  27. }

  28. public class LinkedListTest
  29. {
  30.         public static void main(String[] args)
  31.         {
  32.                 Duilie dl = new Duilie();
  33.                
  34.                 dl.myAdd("java01");
  35.                 dl.myAdd("java02");
  36.                 dl.myAdd("java03");
  37.                 dl.myAdd("java04");
  38.                
  39.                 while(!dl.isNull())
  40.                 {
  41.                         System.out.println(dl.myGet());
  42.                 }
  43.         }
  44. }
复制代码
  1. //ArrayList练习

  2. import java.util.*;
  3. class Person
  4. {
  5.         private String name;
  6.         private int age;
  7.        
  8.         Person(String name, int age)
  9.         {
  10.                 this.name = name;
  11.                 this.age = age;
  12.         }
  13.        
  14.         public boolean equals(Object obj)
  15.         {
  16.                 if(!(obj instanceof Person))
  17.                    return false;
  18.           
  19.            Person p = (Person)obj;
  20.            return this.name.equals(p.name) && this.age == p.age;
  21.         }
  22.        
  23.         public String getName()
  24.         {
  25.                 return name;
  26.         }
  27.        
  28.         public int getAge()
  29.         {
  30.                 return age;
  31.         }
  32. }

  33. public class ArrayListDemo2
  34. {
  35.         public static void main(String[] args)
  36.         {
  37.                 ArrayList al = new ArrayList();
  38.                 al.add(new Person("001",20));
  39.                 al.add(new Person("002",24));
  40.                 al.add(new Person("03",45));
  41.                 al.add(new Person("004",32));
  42.                 al.add(new Person("004",32));
  43.                 al.add(new Person("004",32));
  44.                 al = SingleElement(al);
  45.                 Iterator it = al.iterator();
  46.                
  47.                
  48.                 while(it.hasNext())
  49.                 {
  50.                         Person p = (Person)it.next();
  51.                         System.out.println(p.getName() + "..." + p.getAge());
  52.                 }
  53.         }

  54.         public static ArrayList SingleElement(ArrayList al)
  55.         {
  56.                 ArrayList newal = new ArrayList();
  57.                 Iterator it = al.iterator();
  58.                
  59.                 while(it.hasNext())
  60.                 {
  61.                         Object obj = it.next();
  62.                         if(!newal.contains(obj))
  63.                         {
  64.                                 newal.add(obj);
  65.                         }
  66.                 }
  67.                
  68.                 return newal;
  69.         }       
  70. }
复制代码





  1. <div class="blockcode"><blockquote>/**
  2. TreeSet练习
  3. */
  4. import java.util.*;
  5. class Student implements Comparable
  6. {
  7.         private String name;
  8.         private int age;
  9.        
  10.         Student(String name, int age)
  11.         {
  12.                 this.name = name;
  13.                 this.age = age;
  14.         }
  15.        
  16.         public int compareTo(Object obj)
  17.         {
  18.                
  19.                 if(!(obj instanceof Student))
  20.                         throw new RuntimeException("is not this class");
  21.                 Student s = (Student)obj;
  22.                 if(this.age < s.age)
  23.                         return 1;
  24.                 if(this.age == s.age)
  25.                 {
  26.                         return this.name.compareTo(s.name);
  27.                 }
  28.                 return -1;
  29.         }
  30.        
  31.         public String getName()
  32.         {
  33.                 return name;
  34.         }
  35.        
  36.         public int getAge()
  37.         {
  38.                 return age;
  39.         }
  40. }

  41. public class TreeSetDemo
  42. {
  43.         public static void main(String[] args)
  44.         {
  45.                 TreeSet ts = new TreeSet();
  46.                
  47.                 ts.add(new Student("004",19));
  48.                 ts.add(new Student("003",13));
  49.                 ts.add(new Student("006",17));
  50.                 ts.add(new Student("002",15));
  51.                 ts.add(new Student("0026",15));
  52.                
  53.                 Iterator it = ts.iterator();
  54.                 while(it.hasNext())
  55.                 {
  56.                         Object obj = it.next();
  57.                         Student s = (Student)obj;
  58.                         System.out.println(s.getName() + "..." + s.getAge());
  59.                 }
  60.                
  61.         }
  62. }
复制代码
  1. //HashSet练习

  2. import java.util.*;
  3. class Person
  4. {
  5.         private String name;
  6.         private int age;
  7.        
  8.         Person(String name, int age)
  9.         {
  10.                 this.name = name;
  11.                 this.age = age;
  12.         }
  13.        
  14.         public int hashCode()
  15.         {
  16.                 return this.name.hashCode() +age*23;
  17.         }
  18.         public boolean equals(Object obj)
  19.         {
  20.                 /* if(!(obj instanceof Person))
  21.                    return false;
  22.           
  23.            Person p = (Person)obj;
  24.            return this.name.equals(p.name) && this.age == p.age; */
  25.            return true;
  26.         }
  27.        
  28.         public String getName()
  29.         {
  30.                 return name;
  31.         }
  32.        
  33.         public int getAge()
  34.         {
  35.                 return age;
  36.         }
  37. }

  38. public class HashCodeDemo
  39. {
  40.         public static void main(String[] args)
  41.         {
  42.                 HashSet hs = new HashSet();
  43.                
  44.                 hs.add(new Person("java1", 12));
  45.                 hs.add(new Person("java2", 13));
  46.                 hs.add(new Person("java2", 13));
  47.                 hs.add(new Person("java3", 14));
  48.                
  49.                 Iterator it =hs.iterator();
  50.                
  51.                 while(it.hasNext())
  52.                 {
  53.                         Person  p = (Person)it.next();
  54.                         System.out.println(p.getName() +"..."+p.getAge());
  55.                 }
  56.         }
  57. }
复制代码





----------android培训java培训、java学习型技术博客、期待与您交流!------------


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马