📄 intarrayshared.java
字号:
package examples.cloning;
/** An example class used to demonstrate what
* happens when the default Object.clone method
* is not overridden in a class with object
* reference fields.
*/
public class IntArrayShared implements Cloneable {
private int[] a;
/** Class constructor method
* @param size The maximum number of elements
* @param initValue The initial value given
*/
public IntArrayShared( int size, int initValue ) {
a = new int[size];
for( int i=0; i < a.length; i++ ) {
a[i] = initValue;
}
}
/** Obtain the value of an array element
* @param index the array index of interest
* @return the value at the specified index
*/
public int elementAt( int index ) {
return a[index];
}
/** Set the value of an array element
* @param index the array index to be updated
* @param newValue the new value given
*/
public void setValue( int index, int newValue ) {
a[index] = newValue;
}
/** Converts the object into a String
* @return The value of the array as a String
*/
public String toString() {
StringBuffer sb = new StringBuffer( "[ " );
for( int i=0; i < a.length; i++ ) {
sb.append( a[i] + " " );
}
return sb.append( "]" ).toString();
}
/** The test method for the class
* @param args Not used
* @exception CloneNotSupportedException
* The inherited clone method may
* throw this exception
*/
public static void main( String[] args )
throws CloneNotSupportedException {
IntArrayShared x = new IntArrayShared( 5, 0 );
IntArrayShared y = (IntArrayShared) x.clone();
System.out.println( x );
System.out.println( y );
x.setValue( 2, 9999 );
System.out.println( x );
System.out.println( y );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -