Java learning twelfth day

1. set: may store different types of objects, as the number of objects increases, the capacity is automatically increased (the top layer is an interface)

2. Why are there so many collections?
    Each set of underlying data structure used is not the same, that is to say the characteristics of each set of stored data is not the same,
  items to select a different set of data according to the characteristics

3. To achieve the object of different types of storage?
  Object [] = ARR new new Object [. 3];
    // common methods: Object type defined array can be achieved, but with the increase in the object storage capacity of the array is fixed
    @ disadvantages: a fixed capacity

4. The collection system is the top level of the Collection interface : This interface defines the entire system is the most common thing

The most common features:
  Add:
    boolean the Add (Object O) // set of multi-state objects are stored, the collection is stored in the address of the object
    boolean addAll (Collection c <extends E ?>)
  Delete:
    boolean the Remove (Object O)
    <?> Boolean removeAll (Collection C)
    void Clean ()
  determines:
    Boolean the contains (Object O)
    <?> Boolean acontainsAll (Collection C)
    Boolean isEmpty () // empty judgment
    boolean equals (Object o) // rewritable Object of the equals () method whether the data, the comparison function is stored as a set of two
  acquisition:
    the Iterator <E> Iterator () // get the object set, the set of traversal methods, this method returns the object belongs Iterator interface class implements
    // obtained using the method of internal iterator class object set
    int size () // Get the number of objects in the collection
  set variables group:
    object [] toArray () // set has several objects, the array length is the number

 

1  with Example 1:
 2  Import Classes in java.util *. ;
 . 3  class Demo1 
 . 4  {
 . 5  public  static  void main (String [] args) 
 . 6  {
 . 7 Collection Coll = new new the ArrayList (); // polymorphism 
. 8 coll.add ( "java01 "); // add an object 
. 9 coll.add (" java02 " );
 10 coll.add (" java03 " );
 . 11 coll.add (66); // int new new Integer (66) autoboxing 
12 is  
13 is  Coll. the Add (P);
 14  
15  SOP (Coll);
16  
. 17 Collection C = new new the ArrayList ();
 18 is c.add ( "Hadoop" );
 . 19 c.add ( "Spark" );
 20 is  
21 is coll.addAll (C); // Add the data set to another in the current set 
22 is  
23 is  SOP (Coll);
 24  
25 coll.remove (66); // delete an object 
26 is  SOP (Coll);
 27  
28  coll.removeAll (C);
 29  SOP (Coll);
 30  
31 is  Boolean Boo coll.contains = ( "java01"); // determines whether an object contains 
32  SOP (Boo);
 33 is Boo = coll.containsAll (C);
34 is  SOP (Boo);
 35  
36  Boolean E = coll.isEmpty ();
 37 [  SOP (E);
 38 is  
39 SOP (coll.size ()); // get the number of objects in the collection 
40  
41 is  
42 is Collection D = new new the ArrayList ();
 43 is d.add ( "java01" );
 44 is d.add ( "java02" );
 45 d.add ( "java03" );
 46 is  
47 E = coll.equals (D); // rewrite equals Object in () method, whether the data comparison function is stored as a set of two 
48  SOP (E);
 49  
50  
51 is  // set variables group: Object [] toArray () traversal 
52Object [] = ARR coll.toArray (); // set with a length of several objects, the array is the number 
53 is  for ( int I = 0; I <arr.length; I ++ ) {
 54 is  SOP (ARR [I]) ;
 55  }
 56 is  }
 57 is  
58  public  static  void SOP (Object obj) {
 59  System.out.println (obj);
 60  }
 61 is }

 

1  with Example 2:
 2  Import Classes in java.util *. ;
 . 3  class Demo2 
 . 4  {
 . 5  public  static  void main (String [] args) 
 . 6  {
 . 7  // get the object set Iterator <E> iterator (): This method returns class the object belongs implements Iterator interface
 8  // use the method results in a set of internal iterator class object 
. 9  
10 collection Coll = new new the ArrayList ();
 . 11  
12 is coll.add ( "java01" );
 13 is coll.add ( "java02" ) ;
 14 coll.add ( "java03" );
 15  
16 Iterator ite=coll.iterator();//迭代器
17 
18 while(ite.hasNext()){
19 Object obj = ite.next();//多态
20 String ss = (String)obj;
21 System.out.println(ss.toUpperCase());
22 }    
23 }
24 }

 

5.list Interface: stored data are ordered (sequential order is the order data set and adding data stored is the same), the stored data can be repeated

3 6.list subclass of: the ArrayList: underlying data structures is an array, thread-safe, fast search speed, slow additions (open memory array is continuous) the Vector: underlying data structure uses an array, the thread security and look fast, slow additions and deletions, ArrayList was replaced linkedList: the underlying data structure is the list, thread-safe, look for slow and fast speed deletions
  
  
  


7.list unique features: all functions can be operated at the subject of
  growth:
    void the Add (int index, E Element)
    boolean addAll (<? The extends E> int index, Colletion c)

  Delete:
    E the Remove (int index) // return the object to be deleted

  改:
    E set(int index,E element)

  Zha:
    // the ListIterator provides a set of functions to add, modify and delete, add, modify collection can delete the iterative process implemented in
    the ListIterator <E> the ListIterator
    // iterator returns a list of elements in this list (in proper sequence)
    the ListIterator <E> ListIterator (int index)

    Lis<E> subList(int fromIndex,int lastIndex)//截取指定list,[x,y)
    E get(int index))


8.Iterator iterator: get the iterator is already clear elements in the collection of
   the collection to add, modify the collection iterative process, delete all raise an exception ConcurrentModificationException


9.集合的遍历:Iterator iterator()
  因为数据存储在集合的内部,内部类可以直接操作集合内部的数据
  所以集合使用内部类来遍历数据,因为不同的集合存储数据的结构不同
  所以遍历的方式也不同,所以对所有的内部类提取共性的行为
  定义在一个接口中,这个借口就是Iterator,共性的方式就是
  boolean hasNext()
  object next()
  因此内部类都实现了Iterator接口的


10ListIterator迭代器:提供了对集合进行添加修改和删除的功能,可以实现在迭代过程中对集合进行添加修改和删除

 1 LinkedList自己特有的功能:
 2 1:
 3 addFirst() 添加到链表的头
 4 addLast() 添加到链表的尾
 5 
 6 
 7 2:getFirst() 得到链表的头节点
 8 getLast() 得到链表的尾节点
 9 
10 3:removeFirst() 删除链表的头节点
11 removeLast() 删除链表的尾节点
12 
13 从jdk1.6出现的
14 
15 offerFirst()
16 offerLast()
17 
18 peekFirst()//集合为空返回null,不会异常
19 peekLast()//集合为空返回null,不会异常
20 
21 pollFirst()//集合为空返回null,不会异常
22 pollLast()//集合为空返回null,不会异常

11.遍历集合的五种方法:

 1 1.Iterator ite = list.iterator();
 2 while(ite.hasNext()){
 3 Object obj =ite.next();
 4 sop(obj);
 5 }
 6 
 7 2.for(Iterator it=list.iterator();it.hasNext();){
 8 Object o = it.next();
 9 sop(o);
10 }
11 
12 3.for(int i=0;i<list.size();i++){
13 sop(list.get(i));
14 }
15 
16 
17 4.while(!list.isEmpty()){
18 sop(list.remove(0));
19 }
20 
21 5. //实现对集合的遍历
22 Enumeration en = v.elements();
23 while(en.hasMoreElements()){
24 Object obj = en.nextElement();
25 System.out.println(obj);
26 }

 


12.使用LinkedList实现队列

 1 import java.util.*;
 2 //实现队列
 3 class MyQueue
 4 {
 5 private LinkedList link;
 6 
 7 MyQueue(){
 8 link=new LinkedList();
 9 }
10 
11 //进队
12 public void enQueue(Object obj){
13 link.addLast(obj);
14 }
15 
16 //出队
17 public Object deQueue(){
18 return link.removeFirst();
19 }
20 
21 //判断队列是否为空
22 public boolean isNull(){
23 return link.isEmpty();
24 }
25 }
26 
27 class Demo7 
28 {
29 public static void main(String[] args) 
30 {
31 MyQueue q=new MyQueue();
32 q.enQueue("1");
33 q.enQueue("2");
34 q.enQueue("3");
35 
36 while(!q.isNull()){
37 System.out.println(q.deQueue());
38 }
39 }
40 }

Guess you like

Origin www.cnblogs.com/demain/p/11318576.html