📄 readonlyexample.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -