如何在Java中实现整数数组的复制构造函数?(学校作业)

How to implement a copy constructor for an array of integers in Java? (school assignment)

提问人:user13508239 提问时间:5/11/2020 最后编辑:Basil Bourqueuser13508239 更新时间:5/11/2020 访问量:114

问:

我想使用复制构造函数创建此 IntArrayList 类的深层副本。我写了一个空的复制构造函数,但不知道该放什么。我必须完成这项作业,并证明原件和副本的结果是独立的。

此外,我们不允许使用库方法,例如 or 等。Arrays.copyOf()System.arrayCopy()

任何帮助将不胜感激,因为我一直在这门课上苦苦挣扎。

public class IntArrayList {
    /** 
     * Array that holds all integers
     */
       int[] integersArray;

    /** 
     * Sets initial size of integersArray
     */
       int initialSize = 4;

    /** 
     * Keeps track of the number of elements added and removed from integersArray
     */
       int currentSize = 0;

    public IntArrayList(IntArrayList arrayCopy){

    }

    public static void main(String[] args) {

       IntArrayList v = new IntArrayList(4);

       //Manually tests the implementation of the methods
       v.add(0);
       v.add(1);
       v.add(2);
       v.add(3);
       v.add(4);
       v.add(5);
       v.add(6);
       v.add(7);
       v.add(8);
       v.add(9);
       v.add(10);
       v.set(0,5);
       v.set(1,1001);
       v.set(2,-1001);
       v.remove(0);
       v.remove(2);
       v.remove(4);
       v.add(27);
       v.remove(2);
       v.toString();

    }

    /** 
     * Doubles the size of integersArray to accomodate more values 
     */ 
       private void expandArray() {
           int[] temporary;

          temporary = new int[integersArray.length*2];

              for(int i=0; i<integersArray.length;i++){
                  temporary[i]=integersArray[i];
          }
         int[] temporaryTwo = new int[currentSize+1];

          for(int i=0; i<temporaryTwo.length;i++){
              temporaryTwo[i] = temporary[i];
          }
           integersArray = temporaryTwo;
       }

    /**
     * Constructs the array of integers and defines its initial size    
     * @param initialSize the integer for the starting size of the internal array
     */
       public IntArrayList(int initialSize){
               integersArray = new int [initialSize];

       }

    /**
     * Adds a value to the end of the vector
     * @param val the element that will be added to the internal array
     */
       public void add(int val){



          if(currentSize == integersArray.length){

                  expandArray();

              integersArray[currentSize] = val;

          }else{
         int[] temporary = new int[integersArray.length];

         for(int i=0;i<currentSize;i++){

              temporary[i]=integersArray[i];           
                 }
         temporary[currentSize] = val;

         integersArray = temporary;
          }
          currentSize++;


       }

    /**
     * Removes the last known element in the vector
     */
       public void removeLast(){

               currentSize--;

           if(currentSize == integersArray.length){

                  int[] temporary = new int[integersArray.length-1];

              for(int i=0;i<integersArray.length-1;i++){
                      temporary[i] = integersArray[i];
              }

              integersArray = temporary;

           }else{
              int[] temporary = new int[integersArray.length-1];

          for(int i=0;i<currentSize;i++){
              temporary[i] = integersArray[i];
          }
                  integersArray = temporary;
              System.out.println(temporary);
           }

       }

    /**
     * Gets the value of any integer within integersArray
     * @param index the value being obtained from the internal array 
     */
       public int get(int index){
               return integersArray[index];
       }

    /**
     * Sets any index in integersArray to any value
     * @param index the location of the element that is being set
     * @param val the element which index is being set to
     */
       public void set(int index, int val) {
               integersArray[index] = val;
       }

    /** 
     * Removes the element at index and shifts all elements after it to the left
     * in the internal array
     * @param index the location of the element that is being removed
     */
       public void remove(int index) throws ArrayIndexOutOfBoundsException{


              int[] temporary = new int[integersArray.length];

          for(int i = 0; i<integersArray.length; i++){
          temporary[i] = integersArray[i];
          }


          for(int i = index+1; i<currentSize;i++){
              temporary[i-1] = integersArray[i];
          }
          integersArray = temporary;
          currentSize--;
       }

    /** 
     * Returns number of elements in the vector
     */
       public int size(){
       int size = currentSize;

       return size;
       }

    /** 
     * Converts and prints out a string representation of the current
     * state of the vector
     */
       @Override
       public String toString(){


       int[] temporary = new int[currentSize];

       for(int i=0; i<currentSize; i++){
           temporary[i] = integersArray[i];
       }

       integersArray = temporary;

       String finalArray = Arrays.toString(integersArray);

       System.out.print("[");

       for(int i=0; i<currentSize; i++){
           System.out.print(integersArray[i] + "," + " ");
               if(i == currentSize-1){
               System.out.print(integersArray[i]);

                }
        }
       System.out.print("]");

       return finalArray;
       }
}
Java 数组 copy-constructor deep-copy

评论

1赞 Alan Deep 5/11/2020
你到底想达到什么目的?
0赞 user13508239 5/11/2020
我想创建一个复制构造函数来制作 IntArrayList 的深层副本
0赞 Alan Deep 5/11/2020
还是不清楚。为什么它应该是一个构造函数。有clone()
0赞 user13508239 5/11/2020
这只是我大学课程作业的一部分

答:

1赞 RaffleBuffle 5/11/2020 #1

以下是在不使用 clone 或任何其他内置数组方法的情况下执行此操作的方法。

public IntArrayList(IntArrayList arrayCopy){
  currentSize  = arrayCopy.currentSize;

  integersArray = new int[arrayCopy.integersArray.length];
  for(int i=0; i<currentSize; i++)
    integersArray[i] = arrayCopy.integersArray[i];
}

请注意,您可以在此构造函数中进行选择。您可以创建与 ()完全相同大小的新数组,即 ,在这种情况下,您必须在添加项目时增加它,也可以为其提供与 相同的容量,即 。我在这里展示了后一个选项。arrayCopyarrayCopy.currentSizearrayCopyarrayCopy.integersArray.length

另外,看看你的方法 - 我认为当当前数组中有空间时,没有必要分配一个新数组。这将是一个巨大的性能打击。同样,对于 .addremoveLast

评论

0赞 user13508239 5/11/2020
感谢您的回复,但是有没有办法在不使用的情况下做到这一点?另外,是的,我们不允许使用 or ..clone()Arrays.copyOfSystem.arrayCopy
0赞 user13508239 5/11/2020
非常感谢!感谢您让我知道 和 方法。我刚开始编码,已经这样做了太久了,所以我知道这绝对不是最佳的。addremoveLast