Java高级部分总结

1、数据结构

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

枚举、位集合、向量、栈、字典、哈希表、属性。

 

枚举 Enumeration 

Enumeration接口定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素。下面总结了一些Enumeration声明的方法:

1、boolean hasMoreElements( )

 测试此枚举是否包含更多的元素。

2、Object nextElement( )

如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。

例:

package Test_hello;

import java.util.Vector;

import java.util.Enumeration;   //导入Enumeration接口

public class Hello {

             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

 

向量Vector

Vector 类实现了一个动态数组,主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。

例:

package Test_hello;

import java.util.Vector;

import java.util.Enumeration;   //导入Enumeration接口

 

public class Hello {

             public static void main(String[] args) {    //主函数

                       Vector v = new Vector(3, 2);

                  System.out.println("Initial size: " + v.size());     //0

                  System.out.println("Initial capacity: " + v.capacity());  //3

                  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());  //5

 

                  v.addElement(new Double(5.45));

                  System.out.println("Current capacity: " + v.capacity()); //5

                  v.addElement(new Double(6.08));

                  v.addElement(new Integer(7));

                  System.out.println("Current capacity: " + v.capacity()); //7

                  v.addElement(new Float(9.4));

                  v.addElement(new Integer(10));

                  System.out.println("Current capacity: " + v.capacity());  //9

                  v.addElement(new Integer(11));

                  v.addElement(new Integer(12));

                  System.out.println("First element: " + (Integer)v.firstElement());  //1

                  System.out.println("Last element: " +  (Integer)v.lastElement());  //12

                 

if(v.contains(new Integer(3)))

                  System.out.println("Vector contains 3.");

                   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

 

栈 Stack 

栈是Vector的一个子类,它实现了一个标准的后进先出的栈。

boolean empty()

测试堆栈是否为空。

Object peek( )

查看堆栈顶部的对象,但不从堆栈中移除它。

Object pop( )

移除堆栈顶部的对象,并作为此函数的值返回该对象。

Object push(Object element)

把项压入堆栈顶部。

int search(Object element)

返回对象在堆栈中的位置,以 1 为基数。

例:
package Test_hello;

import java.util.*;

 

public class Hello {

         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);   // stack: [ ]

                 showpush(st, 42);      //push(42)  stack: [42]

                 showpush(st, 66);     //push(66)  stack: [42, 66]

                 showpush(st, 99);     //push(99)  stack: [42, 66, 99]

                 showpop(st);   //pop -> 99  stack: [42, 66]

                 showpop(st);   //pop -> 66  stack: [42]

                 showpop(st);   //pop -> 42   stack: [ ]

                 try {

                     showpop(st);

                 } catch (EmptyStackException e) {  

                     System.out.println("empty stack");  //pop -> empty stack

                 }

          }

}

 

字典 Map

Map类是一个抽象类,用来存储键/值对。

给出键和值,你就可以将值存储在Map对象中。一旦该值被存储,就可以通过它的键来获取它,可以作为一个键/值对列表。

例:

package Test_hello;

import java.util.*;

 

public class Hello {

            public static void main(String[] args) {    //主函数

                      Map m1 = new HashMap();

                 m1.put("Zara", "8");

                 m1.put("Mahnaz", "31");

                 m1.put("Ayan", "12");

                 m1.put("Daisy", "14");

                 System.out.println();

                 System.out.println(" Map Elements");

                 System.out.print("\t" + m1);

          }

}

结果:

 Map Elements

         {Daisy=14, Ayan=12, Zara=8, Mahnaz=31}

 

哈希表 Hashtable 

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

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

哈希表键的具体含义完全取决于哈希表的使用情景和它包含的数据。

例:

package Test_hello;

import java.util.*;

 

public class Hello {

            public static void main(String[] args) {    //主函数

                  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));

 

                 names = balance.keys();        //返回此哈希表中的键的枚举

                 while(names.hasMoreElements()) {

                    str = (String) names.nextElement();

                    System.out.println(str + ": " + balance.get(str));

                   }

 

                 bal = ((Double)balance.get("Zara")).doubleValue();  //返回指定键所映射到的值

                 balance.put("Zara", new Double(bal+1000));    //将指定 key 映射到此哈希表中的指定 value

                 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

 

 

属性 Properties 

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

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

Properties 定义如下实例变量.这个变量持有一个Properties对象相关的默认属性列表。

例:

package Test_hello;

import java.util.*;

 

public class Hello {

            public static void main(String[] args) {    //主函数

                      Properties capitals = new Properties();  //实例化属性类

                 Set states;

                 String str;

                 capitals.put("Illinois", "Springfield");

                 capitals.put("Missouri", "Jefferson City");

                 capitals.put("Washington", "Olympia");

                 capitals.put("California", "Sacramento");

                 capitals.put("Indiana", "Indianapolis");

           

                 states = capitals.keySet();      // get set-view of keys

                 Iterator itr = states.iterator();

                 while(itr.hasNext()) {

                    str = (String) itr.next();

                    System.out.println("The capital of " +

                       str + " is " + capitals.getProperty(str) + ".");  //打印每个结果

                 }

                 

            System.out.println();  //换行

                 str = capitals.getProperty("Florida", "Not Found");   //用指定的键在属性列表中搜索属性。

                 System.out.println("The capital of Florida is " + str + "."); 

                    

          }

}

结果:

The capital of Indiana is Indianapolis.

The capital of Illinois is Springfield.

The capital of Missouri is Jefferson City.

The capital of California is Sacramento.

The capital of Washington is Olympia.

 

The capital of Florida is Not Found.

 

位Bitset

位集合类实现了一组可以单独设置和清除的位或标志。

该类在处理一组布尔值的时候非常有用,你只需要给每个值赋值一"位",然后对位进行适当的设置或清除,就可以对布尔值进行操作了。

例:
package Test_hello;

import java.util.*;

 

public class Hello {

            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("Initial pattern in bits1: ");

                 System.out.println(bits1);     //打印bits1

                 System.out.println("\nInitial pattern in bits2: ");

                 System.out.println(bits2);     //打印bits2

            

                 // AND bits

                 bits2.and(bits1);      // bits1和 bits2 相与,更新bits2

                 System.out.println("\nbits2 AND bits1: ");

                 System.out.println(bits2);

            

                 // OR bits

                 bits2.or(bits1);      // bits2和 bits1 相或,更新bits2

                 System.out.println("\nbits2 OR bits1: ");

                 System.out.println(bits2);

            

                 // XOR bits

                 bits2.xor(bits1);     // bits2和 bits1 相异或,更新bits2

                 System.out.println("\nbits2 XOR bits1: ");

                 System.out.println(bits2);

          }

}

结果:

Initial pattern in bits1:

{0, 2, 4, 6, 8, 10, 12, 14}

 

Initial pattern in bits2:

{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

 

bits2 AND bits1:

{2, 4, 6, 8, 12, 14}

 

bits2 OR bits1:

{0, 2, 4, 6, 8, 10, 12, 14}

 

bits2 XOR bits1:

{}

 

 

 

 

2、泛型

泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。

泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。

例:

package Test_hello;

 

public class Hello {

 

             public static void main(String[] args) {    //主函数

                       // 创建不同类型数组: Integer, Double 和 Character

                      GenericMethodTest GEN  = new GenericMethodTest();

                      Integer[] intArray = { 1, 2, 3, 4, 5 };

                 Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };

                 Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

          

                 System.out.println( "整型数组元素为:" );

                 GEN.printArray(intArray); // 传递一个整型数组

          

                 System.out.println( "\n双精度型数组元素为:" );

                 GEN.printArray(doubleArray); // 传递一个双精度型数组

          

                 System.out.println( "\n字符型数组元素为:" );

                 GEN.printArray(charArray); // 传递一个字符型数组

          }

}

 

 class GenericMethodTest

{

   // 泛型方法 printArray                        

   public static < E > void printArray( E[] inputArray )

   {

      // 输出数组元素           

         for ( E element : inputArray ){       

            System.out.printf( "%s ", element );

         }

         System.out.println();

    }

}

结果:

整型数组元素为:

1 2 3 4 5

 

双精度型数组元素为:

1.1 2.2 3.3 4.4

 

字符型数组元素为:

H E L L O

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值