【问题标题】:How to fill an array with another array in different size?如何用不同大小的另一个数组填充一个数组?
【发布时间】:2020-02-19 22:14:15
【问题描述】:

我有一个第一个 int 数组:

int[] firstarray = {53,45,20,82};
int lengthwanted = 9;
int[] fillarraywith = {15,20};

输出应该是:

finalArray = {53,45,20,82,15,20,15,20,15}

所以根据输入的lengthWanted,我用fillarraywith的值填充第一个数组。

【问题讨论】:

  • 您是否尝试过自己解决这个问题?如果有,可以分享一下吗?

标签: java arrays


【解决方案1】:

将另一个数组复制到原始数组的主要事情可以简单地通过一个 for 循环来完成:

public static int[] fillArrayWithAnother(int[] original, int targetLength, int[] arrayToAppend) {
    // don't forget to handle this case!
    if (targetLength <= original.length) {
        return Arrays.copyOfRange(original, 0, targetLength);
    }

    // this is the new array that we are going to fill
    int[] newArray = new int[targetLength];

    // first fill it with the elements in the original array first
    System.arraycopy(original, 0, newArray, 0, original.length);

    // then starting from where we left off, start the for loop
    for (int i = original.length ; i < targetLength ; i++) {
        // Note the use of the mod operator "%" to create the "cycle" of values
        newArray[i] = arrayToAppend[(i - original.length) % arrayToAppend.length];
    }
    return newArray;
}

【讨论】:

    【解决方案2】:

    您可以使用以下两个步骤:

    //Create a copy of the original array, but with the desired length
    int[] result = Arrays.copyOf(firstarray, lengthwanted);
    
    //for each "new" array elements (beyond the last original element)
    //copy from the filler array:
    IntStream.range(0, lengthwanted - firstarray.length)
        .forEach(i -> result[firstarray.length + i] = 
                          fillarraywith[i % fillarraywith.length]);
    

    【讨论】:

      【解决方案3】:

      这包括 lengthWanted 小于 firstArray 本身长度的情况。

      public static void main(String a[]) {
              int[] firstarray = { 53, 45, 20, 82 };
              int lengthwanted = 9;
              int[] fillarraywith = { 15, 20 };
      
              int[] finalArray = new int[lengthwanted];
              int i = 0;
              for( i= 0; i<lengthwanted && i< firstarray.length; i++) {
                      finalArray[i] = firstarray[i];
      
              }
              for(int k= 0; i<lengthwanted; k=(k+1)%fillarraywith.length) {
                  finalArray[i] = fillarraywith[k];i++;
              }
      
              System.out.println(Arrays.toString(finalArray));
          }
      

      【讨论】:

        【解决方案4】:

        这里是一个考虑java初学者的简单解决方案。

           public static int[] fillArray(int[] original, int targetLength, int[] helperArray)
           {
              // copy the initial array into the target array.
              int[] newArray = Arrays.copyOf(original, targetLength);
        
              // Copy the helperArray elements
              int k = 0; // will be used to access helperArray
              for (int i = original.length; i < targetLength; i++)
              {
                 newArray[i] = helperArray[k++];
                 k %= helperArray.length; // to repeat from first element when k reaches at the end.
        
              }
              return newArray;
           }
        
           public static void main(String[] args)
           {
              int[] firstarray = { 53, 45, 20, 82 };
              int lengthwanted = 9;
              int[] fillarraywith = { 15, 20 };
        
              int[] finalArray = fillArray(firstarray, lengthwanted, fillarraywith);
        
              // to check the result.
              for (int i = 0; i < lengthwanted; i++)
                 System.out.print(finalArray[i] + ", ");
        
           }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-16
          • 2017-09-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多