简体   繁体   English

如何用不同大小的另一个数组填充一个数组?

[英]How to fill an array with another array in different size?

I have a first int array as:我有一个第一个 int 数组:

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

The output should by: output 应该:

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

So depending on the input lengthWanted I fill the firstarray with the values of the fillarraywith.所以根据输入的lengthWanted,我用fillarraywith的值填充firstarray。

The main thing of copying the other array to the original array can be simply done with a for loop:将另一个数组复制到原始数组的主要事情可以通过 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;
}

Here are two steps you can use:您可以使用以下两个步骤:

//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]);

This includes conditions when lengthWanted is lesser than the length of the firstArray itself.这包括当 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));
    }

Here is a simple solution considering a beginner in java.这是一个考虑 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] + ", ");

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM