⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9.txt

📁 电子工业出版社出版的《java2应用开发指南》配套光盘源代码
💻 TXT
字号:

例程9-1
import java.util.*;
class ToArray 
{
    public static void main(String[] args) 
	{
        Vector v = new Vector(Arrays.asList(new String[]{"dog", null}));
        //把向量转化为对象数组序列并打印出来
        Object[] objs = v.toArray();
        print(objs);
   		//把向量转化为字符串数组并打印出来
        String[] strings = (String[])v.toArray(new String[0]);
        print(strings);
        // 在向量中添加整数对象类型的元素并打印

        v.add(new Integer(0));
        print( v.toArray() ); 
    }
    //在屏幕中打印出对象数组序列
    static void print(Object[] arr) 
	{
        System.out.println(Arrays.asList(arr));
    }
}



例程9-2
import java.util.*;
class Contains
{
    public static void main(String[] args) 
	{
     	Vector v = new Vector(Arrays.asList(
		new Object[]{"dog", null, new Integer(7)}));
        System.out.println( v.contains("dog") );         // true
        System.out.println( v.contains(null) );          // true
        System.out.println( v.contains("pig") );         // false
        //注意下面的代码的结果是true
     //这是两个不同的Integer对象,但是这两个Integer对象的equals()方法是相同的
        System.out.println( v.contains(new Integer(7)) );// true
    }
}



例程9-3
//main.java
import java.util.*;
class Main
{
    public static void main(String[] args)
	{
        Vector v = new Vector(Arrays.asList(new String[]{"dog", "cat", "pig"}));
        //抽取向量v中的子串到slist列表中
        List slist = v.subList(1, 2);
        System.out.println( new ArrayList(slist) ); // [cat] 
        System.out.println( slist.get(0) );// cat
        
        // 追加一个字符串元素到sublist列表中.
        slist.add("cow");
        // 在向量中将出现刚添加的字符串元素
        System.out.println( v ); // [dog, cat, cow, pig]
        // 清除sublist中的元素.
        slist.clear();
        // 在向量中不在出现sublist列表中的元素
        System.out.println( v );// [dog, pig]
        // 创建一个sublist列表并在向量中删除一个元素.
        slist = v.subList(0, v.size());
        v.remove(1);
        // 下面的代码将导致ConcurrentModificationException异常
        //System.out.println( slist.get(0) );       
		
    }
}



例程9-4
import java.util.*;
class Main 
{
    public static void main(String[] args) 
{
//
        Date d = new Date();
		 // Sun Jan 10 15:25:03 PST 1999
        System.out.println( d );       
        Date copy = (Date)d.clone();
		 // Sun Jan 10 15:25:03 PST 1999
        System.out.println( copy );    
        // Changing the d does not affect copy.
        d.setTime(123);
		 // Wed Dec 31 16:00:00 PST 1969
        System.out.println( d );       
		 // Sun Jan 10 15:25:03 PST 1999
        System.out.println( copy );    
    } 
}



例程9-5
import java.util.*;

class Main
{
    public static void main(String[] args)
	{
        Hashtable h = new Hashtable();
        h.put("dog", "spot");
        h.put("cat", "luck");
        h.put("cow", "spot");
        h.put("yak", "spot");
        Collection col = h.values();
	    // [luck, spot, spot, spot, ]
        printCol( col );                     
        // true
        System.out.println( col.contains("spot") );  
        // 从集合中删除值为”spot“的表项.
        col.remove("spot");
        // {cat=luck, cow=spot, yak=spot}
        System.out.println( h );             
        //在集合中删除所有值为“spot”的表项
        col.removeAll(Collections.singleton("spot"));
        // {cat=luck}
        System.out.println( h );             
        // 向哈希表中添加一个表项
        h.put("dog", "spot");
        // [luck, spot, ]
        printCol( col );                     
        // 在下面的代码中不能向集合对象中添加元素,
//否则会导致UnsupportedOperationException异常
        //col.add("bess");                   
    }
	//在屏幕上打印集合对象的值
    static void printCol(Collection c) 
	{
        System.out.print("[");
        for (Iterator it=c.iterator(); it.hasNext(); )
		{
            System.out.print(it.next()+", ");
        }
        System.out.println("]");
    }
}



例程9-6
import java.io.*;
import java.util.*;

class PropertiesOutput 
{
    public static void main(String[] args) 
{
        Properties props = new Properties();
        props.setProperty("title", "The Java Class Libraries");
        try 
{
            props.store(new FileOutputStream("out.properties"), "comment");
        }
 catch (IOException e) 
{
            e.printStackTrace();
        }
    } 
}



例程9-7
import java.util.*;
class RandomInt 
{
    public static void main(String[] args) 
{
        Random r = new Random();
        System.out.println( r.nextInt() );
        System.out.println( r.nextInt() );
        System.out.println( r.nextInt(10) );
        System.out.println( r.nextInt(10) );
        // r.nextInt(-10);                      
// IllegalArgumentException
    } 
}



例程9-8
import java.util.*;

class RandomBoolean 
{
    public static void main(String[] args) 
{
        Random r = new Random();
        System.out.println( r.nextBoolean() );
        System.out.println( r.nextBoolean() );
        System.out.println( r.nextBoolean() );
    } 
}



例程9-9
import java.util.*;

class Main 
{
    public static void main(String[] args) 
{
        String[] strs = {"dog", "pig", "cat"};
        Arrays.sort(strs);
		 // [cat, dog, pig]
        System.out.println( Arrays.asList(strs) );    
        int[] ints = {9, 5, 1, 7};
        Arrays.sort(ints);
        for (int i=0; i<ints.length; i++) 
{
	  // 1 5 7 9
            System.out.print( ints[i]+" " );          
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -