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

📄 intarrayshared.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 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 + -