【Java学习系列】第3课--Java 高级教程

    xiaoxiao2023-08-05  169

    java数据结构

     

      1)【概述】

      Java的工具包提供了强大的数据结构在的Java中的数据结构主要包括以下几种接口和类:

    枚举(枚举)

    位集合(位集合)

    向量(矢量)

    栈(栈)

    字典(词典)

    哈希表(哈希表)

    属性(属性)

      以上这些类是传统遗留的,在Java2的中引入了一种新的框架 - 集合框架(集合),我们后面再讨论。

     

      2)枚举(枚举)

        a)【定义】枚举(枚举)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广。枚举(枚举)接口定义了一种从数据结构中取回连续元素的方式

        B)【代码举例】

    import java.util.Vector; import java.util.Enumeration; public class EnumerationTester {public static void main(String args []){        枚举天数;        Vector dayNames = new Vector();        dayNames.add( “星期天”);        dayNames.add( “星期一”);        dayNames.add( “星期二”);        dayNames.add( “星期三”);        dayNames.add( “星期四”);        dayNames.add( “星期五”);        dayNames.add( “星期六”);        days = dayNames.elements(); while(days.hasMoreElements()){           System.out.println(days.nextElement());        }     }  }

    EnumerationTester.java

         运行结果:

    周日 星期一 星期二 星期三 星期四 星期五 星期六

    运行结果

    如果你想学习Java可以来这个群,首先是二二零,中间是一四二,最后是九零六,里面有大量的学习资料可以下载。

     

      3)位集合(位集合)

        A)【定义】

          位集合类实现了一组可以单独设置和清除的位或标志。该类在处理一组布尔值的时候非常有用,你只需要给每个值赋值一 “位”,然后对位进行适当的设置或清除,就可以对布尔值进行操作了。

        B)【代码举例】

    import java.util.BitSet; public class BitSetDemo {public static void main(String args []){       BitSet bits1 = new BitSet(16);       BitSet bits2 = new BitSet(16);            //设置一些位      (int i = 0; i <16; i ++){if((i%2)== 0)bits1.set(i); if((i%5)!= 0)bits2.set(i);       }       System.out.println(“bit1:”中的初始模式);       的System.out.println(BITS1);       System.out.println(“\ nInitial pattern in bits2:”);       的System.out.println(BITS2); // AND bits       bits2.and(bits1);       System.out.println(“\ nbits2 AND bits1:”);       的System.out.println(BITS2); // OR bits       bits2.or(bits1);       System.out.println(“\ nbits2 OR bits1:”);       的System.out.println(BITS2); // XOR bits       bits2.xor(bits1);       System.out.println(“\ nbits2 XOR bits1:”);       的System.out.println(BITS2);    }  }

    BitSetDemo.java

      运行结果:

    位1中的初始模式: { 0,2,4,6,8,10,12,14 }位2中的 初始模式: { 1,2,3,4,6,7,8,9,11,12,13 , 14}  位 2 AND位1:{ 2,4,6,8,12,14 } 位2  OR位1: {  0,2,4,6,8,10,12,14 } 位2 XOR位1: {}

    运行结果

     

     4)向量(Vector)

      A)【定义】

        向量(向量)类和传统数组非常相似,但是矢量的大小能根据需要动态的变化。

        和数组一样,矢量对象的元素也能通过索引访问。

        使用矢量类最主要的好处就是在创建对象的时候不必给对象指定大小,它的大小会根据需要动态的变化。

     

      B)【代码实例】

    import java.util。*; public class VectorDemo {public static void main(String args []){//初始大小为3,增量为2        Vector v = new Vector(3,2);        System.out.println(“初始大小:”+ v.size());        System.out.println(“初始容量:”+        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(“四个添加后的容量:”+            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”); //枚举向量中的元素。       枚举vEnum = v.elements();        System.out.println(“\ nElements in vector:”); while(vEnum.hasMoreElements())          System.out.print(vEnum.nextElement()+“”);        的System.out.println();     }  }

    VectorDemo.java

        运行结果:

    初始容量:0  初始容量:3  四次加载后的容量:5  电流容量:5  电流容量:7  电流容量:9  第一元素:1  最后元素:12  矢量包含3.矢量中的 元素: 1 2 3 4 5.45 6.08 7 9.4 10 11 12

    运行结果

     

     

    5)栈(堆栈)

      A)【定义】

        栈(堆栈)实现了一个后进先出(LIFO)的数据结构。

        你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部。

        当你从栈中取元素的时候,就从栈顶取一个元素。换句话说,最后进栈的元素最先被取出。

     

      B)【代码实例】

    import java.util。*; public class StackDemo {static void showpush(Stack st,int a){        st.push(new Integer(a));        System.out.println(“push(”+ a +“)”);        System.out.println(“stack:”+ st);     } static void showpop(Stack st){        System.out.print(“pop  - >”);        整数a =(整数)st.pop();        的System.out.println(一);        System.out.println(“stack:”+ st);     } public static void main(String args []){        Stack st = new Stack();        System.out.println(“stack:”+ st);        showpush(st,42);        showpush(st,66);        showpush(st,99);        showpop(ST);        showpop(ST);        showpop(ST); 尝试{           showpop(st);        } catch(EmptyStackException e){           System.out.println(“empty stack”);        }     }  }

    StackDemo.java

        运行结果:

    堆栈:[]  推送(42) 堆栈:[42]  推(66) 堆栈:[42,66]  推(99) 堆栈:[42,66,99]  弹出- > 99  堆栈:[42,66]  弹出- > 66  堆栈:[42]  pop  - > 42  堆栈:[]  pop  - >空堆栈

    运行结果

     

    6)字典(词典)

      A)【定义】

        字典(字典)类是一个抽象类,它定义了键映射到值的数据结构。

        当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用字典。

        由于字典类是抽象类,所以它只提供了键映射到值的数据结构,而没有提供特定的实现。

     

    7)哈希表(哈希表)

      A)【定义】  

        Hashtable的类提供了一种在用户定义键结构的基础上来组织数据的手段。

        例如,在地址列表的哈希表中,你可以根据邮政编码作为键来存储和排序数据,而不是通过人名。

     

      B)【代码实现】

    import java.util。*; public class HashTableDemo {public static void main(String args []){//创建一个哈希映射       Hashtable balance = new Hashtable();        枚举名称        字符串str 双平;        balance.put(“Zara”,new Double(3434.34));        balance.put(“Mahnaz”,new Double(123.22));        balance.put(“Ayan”,新双人(1378.00));        balance.put(“Daisy”,new Double(99.22));        balance.put(“Qadir”,new Double(-19.08)); //显示哈希表中的所有余额。       names = balance.keys(); while(names.hasMoreElements()){           str =(String)names.nextElement();           System.out.println(str +“:”+           balance.get(str));        }        System.out.println(); //将1,000存入Zara的帐户       bal =((Double)balance.get(“Zara”))。doubleValue();        balance.put(“Zara”,new Double(bal + 1000));        System.out.println(“Zara的新余额:”+        balance.get(“Zara”));     }  }

    HashTableDemo.java

      运行结果如下:

    Qadir:-19.08  Zara:3434.34  Mahnaz:123.22  Daisy:99.22  Ayan:1378.0  Zara的新余额:4434.34

    运行结果

     

    8)哈希表(哈希表)

      A)【定义】

     

        属性继承于Hashtable.Properties类表示了一个持久的属性集。属性列表中每个键及其对应值都是一个字符串。

     

        属性类被许多Java类使用。例如,在获取环境变量时它们就作为System.getProperties()方法的返回值。

      B)【代码实现】

    import java.util。*; public class PropDemo {public static void main(String args []){        Properties capitals = new Properties();        设置状态        字符串str               capitals.put(“Illinois”,“Springfield”);        capitals.put(“密苏里州”,“杰斐逊城”);        首都(“华盛顿”,“奥林匹亚”);        capitals.put(“California”,“Sacramento”);        capitals.put(“Indiana”,“Indianapolis”); //在哈希表中显示所有州和首都。       states = capitals.keySet(); //获取键的set-view        Iterator itr = states.iterator(); while(itr.hasNext()){           str =(String)itr.next();           System.out.println(“”+              str + “的大写是”+ capitals.getProperty(str)+“。”);        }        System.out.println(); //寻找状态不在列表中 - 指定默认的       str = capitals.getProperty(“Florida”,“Not Found”);        System.out.println(“佛罗里达的首都是”            + str +“。”);     }  }

    PropDemo.java

     

      运行结果如下:

    密苏里州首府是杰斐逊城。 伊利诺伊州首府是斯普林菲尔德。 印第安纳州首府是印第安纳波利斯。 加利福尼亚的首都是萨克拉门托。 华盛顿的首府是奥林匹亚。 佛罗里达州的首府未找到。 相关资源:敏捷开发V1.0.pptx
    最新回复(0)