readonlyexample.java
来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 30 行
JAVA
30 行
package examples.collections;
import java.util.*;
/** A class to demonstrate the use of methods
* to create read-only collections
*/
public class ReadOnlyExample {
/** Test method for the class
* @param args not used
*/
public static void main( String[] args ) {
// create a map and intialize it
SortedMap m1 = new TreeMap();
m1.put( "beans", new Float( 2.99 ) );
m1.put( "carrots", new Float( 1.69 ) );
m1.put( "peas", new Float( 2.19 ) );
m1.put( "cabbage", new Float( 3.29 ) );
m1.put( "squash", new Float( 1.89 ) );
// create a read-only collection, then try to
// change it
m1 = Collections.unmodifiableSortedMap( m1 );
try {
m1.put( "parsnips", new Float( 1.59 ) );
} catch ( UnsupportedOperationException x ) {
System.out.println( x + " caught!" );
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?