zoukankan      html  css  js  c++  java
  • 18、Java中的 数据结构

    Java2 Collection,(,).

    1 (Enumeration)

    1.1 Enumeration

    public interface Enumeration<E> {

       boolean hasMoreElements();

       E nextElement();
    }

    Enumeration

    Enumeration 使使VectorProperties

    1

    import java.util.Vector;
    import java.util.Enumeration;

    public class EnumerationTester {

      public static void main(String args[]) {
         Enumeration<String> days;
         Vector<String> dayNames = new Vector<String>();
         dayNames.add("Sunday");
         dayNames.add("Monday");
         dayNames.add("Tuesday");
         dayNames.add("Wednesday");
         dayNames.add("Thursday");
         dayNames.add("Friday");
         dayNames.add("Saturday");
         days = dayNames.elements();
         while (days.hasMoreElements()){
            System.out.println(days.nextElement());
        }
      }
    }

    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday

    2

        {  
          Map map = request.getParameterMap();
          java.util.Enumeration enum=this.getRequest().getParameterNames();
           while ( enum.hasMoreElements()){
              String paramName = (String) enum.nextElement();
              String[] values = request.getParameterValues(paramName);
               for (int i = 0; i < values.length; i++) {
                  System.out.println("[" + i + "]   " + paramName + " " + values[i]);
              }
          }
          //map  
          map.put(paramName, paramValue);
          }

    1.2

    Java JDK 1.5

      使 enum java.lang.Enum Object

      使 private public

         public static final

       values()

    package cn.fage.enums;

    /**
    * @author lin
    * @version 1.0
    * @date 2020-06-19 17:11
    * @Description TODO
    */
    public enum SeasonTwoArgs {

       /**
        * 
        */
       SPRING(1, ""),
       SUMMER(2, ""),
       AUTUMN(3, ""),
       WINTER(4, "");

       int key;
       String msg;

       SeasonTwoArgs(int key, String season) {
           this.key = key;
           this.msg = season;
           System.out.println(":" + this.name() + "," + this.msg + "," + season);
      }

       //  key 
       public static SeasonTwoArgs valueofKey(Integer key) {
           for (SeasonTwoArgs season : SeasonTwoArgs.values()) {
               if (season.key == key) {
                   return season;
              }
          }
           throw new IllegalArgumentException("No element matches " + key);
      }


       public String getMsg() {
           return msg;
      }

       public int getKey() {
           return key;
      }

       public void setKey(int key) {
           this.key = key;
      }

       public void setMsg(String msg) {

           this.msg = msg;
      }

       public static void main(String[] args) {
           SeasonTwoArgs autumn = SeasonTwoArgs.AUTUMN;
           System.out.println("autumn.getKey() = " + autumn.getKey());
           System.out.println("autumn.getMsg() = " + autumn.getMsg());
           System.out.println("autumn = " + autumn);

      }
    }

    :SPRING,,
    :SUMMER,,
    :AUTUMN,,
    :WINTER,,
    autumn.getKey() = 3
    autumn.getMsg() = 
    autumn = AUTUMN

    2 (BitSet)

    BitsetBitSetvector of bits

    Java 2

    BitSet

    BitSet()

    0

    BitSet(int size)

    package cn.fage.three;

    import java.util.BitSet;

    /**
    * @author lin
    * @version 1.0
    * @date 2020-07-10 14:00
    * @Description TODO
    */
    public class BitSetDemo {
       public static void main(String[] args) {
           BitSet bits1 = new BitSet(16);
           BitSet bits2 = new BitSet(16);
           // set some bits
           for (int i = 0; i < 16; i++) {
               if ((i % 2) == 0) {
                   bits1.set(i);
              }
               if ((i % 5) != 0) {
                   bits2.set(i);
              }
          }
           System.out.println("bits1.length() = " + bits1.length());
           System.out.println("bits2.length() = " + bits2.length());
           System.out.println("bits1.get(4) = " + bits1.get(0));
           System.out.println("bits2.get(4) = " + bits2.get(0));
           System.out.println("bits1 = " + bits1);
           System.out.println("bits2 = " + bits2);


           // void and(BitSet set)
           //  set  set , ()
    //       bits2.and(bits1);
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           // void andNot(BitSet set)
           //  BitSet  BitSet  ()
    //       bits2.andNot(bits1);
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           //int cardinality( )
           // BitSet  true 
    //       System.out.println(bits1.cardinality());
    //       System.out.println(bits2.cardinality());

           //void clear( )
           // BitSet  false
    //       bits1.clear();
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           // void clear(int index)
           //  false
    //       bits1.clear(2);
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           // void clear(int startIndex, int endIndex)
           //  fromIndex toIndex false
           //
    //       bits1.clear(2,6);
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           //   Object clone( )
           //  BitSet BitSet
           //boolean equals(Object bitSet)
           // 
    //       BitSet bits3 = (BitSet) bits1.clone();
    //       System.out.println(bits3);
    //       System.out.println(bits3.equals(bits1));

           //void flip(int index)
           // 
    //       bits1.flip(0);
    //       bits1.flip(2);
    //       bits1.flip(3);
    //       System.out.println(bits1);
    //       System.out.println(bits2);

           //boolean get(int index)
           //  fromIndex toIndex
           // 
    //       System.out.println(bits1.get(4));
    //       System.out.println(bits1.get(5));

           //BitSet get(int startIndex, int endIndex)
           //  BitSet BitSet  fromIndex toIndex
    //       BitSet bits3=bits1.get(2,6);
    //       System.out.println(bits3);

           //int hashCode( )
           //  set 
    //       System.out.println(bits1.hashCode());
    //       System.out.println(bits1.hashCode());

           //boolean intersects(BitSet bitSet)
           //  BitSet  true  BitSet  true true
           /*BitSet bits3 = new BitSet(16);
           System.out.println(bits3);
           bits3.set(0);
           System.out.println(bits3.intersects(bits1));
           System.out.println(bits3.intersects(bits2));
           System.out.println(bits1);
           System.out.println(bits2);
           System.out.println(bits3);*/

           //boolean isEmpty( )
           //  BitSet  true  true
    //       bits1.clear();
    //       System.out.println(bits1);
    //       System.out.println(bits1.isEmpty());

           //...

           //OR bits
           //  set  set 
    //         bits2.or(bits1);
    //         System.out.println("bits2 OR bits1: ");
    //         System.out.println(bits2);

           // BitSet 使
    //       System.out.println(bits1.size());

    //       System.out.println(bits1.toString());
           //  set  set 
    //       bits1.xor(bits2);
    //       System.out.println(bits1);

      }
    }

    3 (Vector)

    Vector java.util

    1  10
    Vector()
    2 
    Vector(int size)
    3  incr 
    Vector(int size,int incr)
    4  c 
    Vector(Collection c)

    package cn.fage.three;

    import java.util.Enumeration;
    import java.util.Vector;

    /**
    * @author lin
    * @version 1.0
    * @date 2020-07-10 14:57
    * @Description TODO
    */
    public class TestVectorDemo {

       public static void main(String args[]) {
           // initial size is 3, increment is 2
           Vector v = new Vector(3, 2);
           System.out.println("Initial size: " + v.size());
           System.out.println("Initial capacity: " +
                   v.capacity());
           v.addElement(new Integer(1));
           v.addElement(new Integer(2));
           v.addElement(new Integer(3));
           v.addElement(new Integer(4));
           System.out.println("Capacity after four additions: " +
                   v.capacity());

           v.addElement(new Double(5.45));
           System.out.println("Current capacity: " +
                   v.capacity());
           v.addElement(new Double(6.08));
           v.addElement(new Integer(7));
           System.out.println("Current capacity: " +
                   v.capacity());
           v.addElement(new Float(9.4));
           v.addElement(new Integer(10));
           System.out.println("Current capacity: " +
                   v.capacity());
           v.addElement(new Integer(11));
           v.addElement(new Integer(12));
           System.out.println("First element: " +
                  (Integer) v.firstElement());
           System.out.println("Last element: " +
                  (Integer) v.lastElement());
           if (v.contains(new Integer(3)))
               System.out.println("Vector contains 3.");
           // enumerate the elements in the vector.
           Enumeration vEnum = v.elements();
           System.out.println("nElements in vector:");
           while (vEnum.hasMoreElements()) {
               System.out.print(vEnum.nextElement() + " ");
          }
           System.out.println();
      }
    }

    Initial size: 0
    Initial capacity: 3
    Capacity after four additions: 5
    Current capacity: 5
    Current capacity: 7
    Current capacity: 9
    First element: 1
    Last element: 12
    Vector contains 3.

    Elements in vector:
    1 2 3 4 5.45 6.08 7 9.4 10 11 12

    4 (Stack)

    Vector

    Vector

    Stack()

    1.Stack

    FILOFirst In Last Out

    Stack线线

    (Top)(Bottom)

    package cn.fage.three;

    import java.util.EmptyStackException;
    import java.util.Stack;

    /**
    * @author lin
    * @version 1.0
    * @date 2020-07-10 15:00
    * @Description TODO
    */
    public class TestStackDemo {

       static void showpush(Stack<Integer> st, int a) {
           st.push(new Integer(a));
           System.out.println("push(" + a + ")");
           System.out.println("stack: " + st);
      }

       static void showpop(Stack<Integer> st) {
           System.out.print("pop -> ");
           Integer a = (Integer) st.pop();
           System.out.println(a);
           System.out.println("stack: " + st);
      }

       public static void main(String args[]) {
           Stack<Integer> st = new Stack<Integer>();
           System.out.println("stack: " + st);
           showpush(st, 42);
           showpush(st, 66);
           showpush(st, 99);
           showpop(st);
           showpop(st);
           showpop(st);
           try {
               showpop(st);
          } catch (EmptyStackException e) {
               System.out.println("empty stack");
          }
      }
    }

    stack: []
    push(42)
    stack: [42]
    push(66)
    stack: [42, 66]
    push(99)
    stack: [42, 66, 99]
    pop -> 99
    stack: [42, 66]
    pop -> 66
    stack: [42]
    pop -> 42
    stack: []

    5 (Dictionary)

    Dictionary /Map

    DictionaryMap Dictionary /

    Dictionary[Map/

    6 (Hashtable)

    Hash table(Key value)访访Mf(key)keyM(Hashf(key)(Hash)


    public class HashTableDemo {

      public static void main(String args[]) {
         // Create a hash map
         Hashtable balance = new Hashtable();
         Enumeration names;
         String str;
         double bal;

         balance.put("Zara", new Double(3434.34));
         balance.put("Mahnaz", new Double(123.22));
         balance.put("Ayan", new Double(1378.00));
         balance.put("Daisy", new Double(99.22));
         balance.put("Qadir", new Double(-19.08));

         // Show all balances in hash table.
         names = balance.keys();
         while(names.hasMoreElements()) {
            str = (String) names.nextElement();
            System.out.println(str + ": " +
            balance.get(str));
        }
         System.out.println();
         // Deposit 1,000 into Zara's account
         bal = ((Double)balance.get("Zara")).doubleValue();
         balance.put("Zara", new Double(bal+1000));
         System.out.println("Zara's new balance: " +
         balance.get("Zara"));
      }
    }

    Qadir: -19.08
    Zara: 3434.34
    Mahnaz: 123.22
    Daisy: 99.22
    Ayan: 1378.0

    Zara's new balance: 4434.34

    7 (Properties)

    java.util.Properties Hashtable 使Java使 System.getProperties Properties

    PropertiesHashtableMap使
    Propertieskeyvalue
    IO

    Properties

    /
    setProperty(String key,String value);
    getProperty(String key)

    load(InputStream inStream)

    load(Reader reader)

    test.txt.

    name=
    age=18
    county=
    package FileAndIO;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.Set;

    public class PropertiesDemo {
       public static void main(String[] args) throws IOException {
           //properties
           Properties properties = new Properties();
           FileInputStream fileInputStream = new FileInputStream("Demo//test.txt");
           //load
           properties.load(fileInputStream);
           //stringPropertyNameskeyset
           Set<String> keys = properties.stringPropertyNames();
           //set
           for (String key : keys) {
               System.out.println(key+":"+properties.getProperty(key));
          }
      }
    }


                    

    圈~

     注公众号

  • 相关阅读:
    【设计模式】六大原则
    【HTML5】表单属性
    【HTML5】表单元素
    【HTML5】input类型
    【HTML5】Server-Sent服务器发送事件
    【HTML5】Web Workers
    【HTML5】Application Cache应用程序缓存
    【HTML5】Web存储
    【HTML5】地理定位
    【HTML5】Canvas和SVG的区别
  • 原文地址:https://www.cnblogs.com/naimao/p/13353333.html
Copyright © 2011-2022 走看看