【问题标题】:Copy arrays Java复制数组 Java
【发布时间】:2013-11-15 14:20:48
【问题描述】:

我有一点问题:

 private void resize(int i) {                         
   this.capacity +=i;                           // new capacity
   this.memoryaux = new Integer[this.capacity]; // create array aux with new capacity
   for (int a = 0; a < this.capacity; a++)
     this.memoryaux[a] = null;                  // initialize null
   this.memory = this.memoryaux;                // copy array aux into my array             
 } 

我想调整数组内存的大小,例如容量为 2。但它已经在里面了,所以我试图用新容量创建一个新数组 (memoryaux),然后复制旧中新。

我的问题是我丢失了旧数组中的所有内容,如何在不丢失内部所有内容的情况下做到这一点?一个例子是这样的:

容量为 2 的旧数组有 [0]1 [1]2。

创建更大容量为 4 的新数组。

然后改变旧数组的容量,完成如下: [0]1 [1]2 [2]null [3]null

【问题讨论】:

标签: java arrays copy


【解决方案1】:

您正在寻找public static T[] Arrays.copyOf(T[] original,int newLength)

您需要指定一个大于旧长度的新长度。这会将您现有的数组复制到一个新的更大的数组中。

// Intializing an array of length 3
int[] array = new int[] {45, 32, 75};
// Copying array with newlength as 5
array = Arrays.copyOf(array, 5);
array[3] = 11;
array[4] = 55;   

或 - 在 1.6 之前的 Java 中 public static void System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

// Intializing an array of length 3
int[] array = new int[] {45, 32, 75};
// Copying array with newlength as 5
int[] biggerArray = new int[5];
System.arraycopy(array, 0, biggerArray, 0, array.length);
biggerArray [3] = 11;
biggerArray [4] = 55;   

【讨论】:

    【解决方案2】:

    您正在重新分配 old-array-reference-variable 以指向具有较大大小的 new-array-object,这意味着对 old-array-object 的引用丢失并且可能被垃圾回收

    您需要做的是将old-array-object 的内容复制到新的数组对象,然后再分配给old-array-reference

    要复制数组:你可以这样做:

    class ArrayCopyDemo {
        public static void main(String[] args) {
            char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
                    'i', 'n', 'a', 't', 'e', 'd' };
            char[] copyTo = new char[7];
    
            System.arraycopy(copyFrom, 2, copyTo, 0, 7);
            System.out.println(new String(copyTo));
        }
    }
    

    【讨论】:

      【解决方案3】:

      在您的代码 sn-p 中,您永远不会将现有值复制到 memoryaux。您只是将 nulls 分配给辅助数组中的所有索引。

      【讨论】:

        【解决方案4】:

        我认为您应该需要将一个 memory 数组附加到 memoryaux 数组,然后尝试复制到 memory 数组。 tgThat 将解决此问题。

        private void resize(int i) {                         
           this.capacity +=i;                           // new capacity
           this.memoryaux = new Integer[this.capacity + this.memory.length]; // create array aux with new capacity
           for (int a = 0; a < this.memory.length; a++)
             this.memoryaux[a] = this.memory[a];                  // initialize null
           for (int a = 0; a < this.capacity; a++)
             this.memoryaux[this.memory.length + a] = null;                  // initialize null
           this.memory = this.memoryaux;                // copy array aux into my array             
         } 
        

        【讨论】:

          【解决方案5】:

          首先,该方法可以重命名为increaseSize(int i)。

          问题是您忘记将旧数组的值复制到新数组中。在最后一行中,this.memory 会影响新数组的地址,但仅此而已,因此旧数组及其内容会丢失。

          【讨论】:

            【解决方案6】:

            将旧数组复制到新数组(this.memory = this.memoryaux; 之前:

            System.arrayCopy(this.memory, 0, this.memoryAux, 0, this.memory.length);
            

            不要将新的数组元素初始化为 null...这已经由 JVM 完成了。

            【讨论】:

              【解决方案7】:

              这段代码是错误的

              this.memory = this.memoryaux;  //copy array aux into my array  
              

              这不是抄袭!这是覆盖。内存阵列中的原始数据丢失了。

              【讨论】:

                【解决方案8】:

                试试这个..

                公共类工具{ 整数容量 = 2; int[] memory = new int[容量];

                private void resize(int i) {                         
                    System.out.println(Arrays.toString(memory));
                    this.capacity +=i; 
                    int temp [] = memory;
                    memory = new int[capacity];
                    for(int k = 0; k<temp.length; k++){
                        memory[k] = temp[k];
                    }
                    System.out.println(Arrays.toString(memory));
                } 
                public static void main(String[] a){
                     new Tool().resize(8);
                }
                

                }

                【讨论】:

                  【解决方案9】:

                  您可以使用Arrays.copyOf 调整数组大小。

                  让我们先开始一个例子:

                  Integer[] dataArray = new Integer[10];
                           for(int i=0;i<10;i++)
                      {
                          dataArray[i] = i;
                      }
                  
                      //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
                      System.out.printf("Old capacity ==> %s \n", Arrays.toString(dataArray));
                  
                  
                      int oldCapacity = dataArray.length;
                  
                      //newCapacity = 2 * oldCapacity
                          int newCapacity = oldCapacity <<1;
                  
                      //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null, null, null, null, null, null, null, null, null, null] 
                      dataArray = Arrays.copyOf(dataArray, newCapacity);
                  
                      System.out.printf("New capacity ==> %s \n", Arrays.toString(dataArray));
                  

                  通过上面的例子,一行代码就可以实现resize方法,比如,

                   private void resize(int i) {                         
                         this.memory = Arrays.copyOf( this.memory, capacity+i);
                       } 
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-09-17
                    • 1970-01-01
                    • 2012-05-12
                    • 2023-03-20
                    • 2012-04-25
                    • 1970-01-01
                    相关资源
                    最近更新 更多