📄 intarray.java
字号:
package examples.cloning;
/** An example class used to demonstrate proper cloning
* concepts
*/
public class IntArray implements Cloneable {
private int[] a;
/** Class constructor method
* @param size The maximum number of elements
* @param initValue The initial value given
*/
public IntArray( 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;
}
/** Convert the object to a String
* @return The value of the array as a String
* object
*/
public String toString() {
StringBuffer sb = new StringBuffer( "[ " );
for( int i=0; i<a.length; i++ ) {
sb.append( a[i] + " " );
}
return sb.append( "]" ).toString();
}
/** Provide a clone method specifically for this
* class
* @exception CloneNotSupportedException
* Superclass may throw this
* @return A clone of the object
*/
public Object clone()
throws CloneNotSupportedException {
IntArray newObject = (IntArray)super.clone();
newObject.a = (int[])a.clone();
return newObject;
}
/** The test method for the class
* @param args Not used
* @exception CloneNotSupportedException
* The overiding clone method may
* throw this
*/
public static void main( String[] args )
throws CloneNotSupportedException {
IntArray x = new IntArray( 5, 0 );
IntArray y = (IntArray)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 + -