【问题标题】:Return new Array that adds up every 2 values?返回每 2 个值相加的新数组?
【发布时间】:2020-09-21 13:13:24
【问题描述】:

我正在尝试创建一个方法,该方法将接收一个数组并返回一个新数组,该数组将每 2 个值相加以创建一个新值并继续下一个 2 个值。如果我有一个 {5,8,4,2} 数组,我希望该数组变为 {13,6}。到目前为止,在我的代码中,我已将其设置为创建 {5,8,4,2} 数组并将其变为 {13,12,6,0} 但是,我不希望它添加大大地。所以我想知道如何让我的程序在第一次加法后跳过第二个值?还有,最后怎么去掉那个没用的零呢?

import java.util.*;
import java.io.*;
public class reduceArray{
   public static int[] reduce(int[] currentArray){
   int[] newArray = new int[currentArray.length];

   for(int i = 0; i < currentArray.length -1; i++){


     newArray[i] = currentArray[i] + currentArray[i+1];


      }

     return newArray;

   }

   public static void main(String[] args){
       int[] arrayANew;
       int[] arrayA = {5,8,4,2};



       arrayANew= reduce(arrayA);



       System.out.println(Arrays.toString(arrayANew) );




    }
}

【问题讨论】:

  • 也许您想增加索引以跳过所有其他元素?
  • 我最初拥有它,所以 for 循环每次都会加 2,但这有点奏效,但在新数组中,下一个索引只会返回零。示例:{15, 20, 30, 1} 的数组将返回为 {35, 0, 50, 0, 31}
  • 类似的问题,但专注于使用 Java Streams 的解决方案:Java Stream: is there a way to iterate taking two elements a time instead of one?

标签: java arrays for-loop return


【解决方案1】:

您没有具体说明当前数组是否总是长度为偶数,但我们可以轻松处理奇数长度的数组:

public static int[] reduce(int[] currentArray) {
    int[] newArray = new int[(currentArray.length + 1) / 2];

    for(int i=0; i<currentArray.length; i++) {
        newArray[i/2] += currentArray[i];
    }

    return newArray;
}

测试:

System.out.println(Arrays.toString(reduce(new int[] {5, 8, 4, 2})));
System.out.println(Arrays.toString(reduce(new int[] {5, 8, 4, 2, 7})));

输出:

[13, 6]
[13, 6, 7]

【讨论】:

    【解决方案2】:

    只需要以不同于旧数组的方式递增新数组,如下所示。 (我还添加了更改它以能够处理奇数长度的数组并使 newArray 具有正确的长度)

    import java.util.*;
    import java.io.*;
    class Scratch{
        public static int[] reduce(int[] currentArray){
            int[] newArray = new int[(int) Math.ceil((float) currentArray.length / 2)];
    
            if (newArray.length % 2 != 0) newArray[newArray.length - 1] = currentArray[currentArray.length - 1];
    
            for(int i = 0, j = 0; i < currentArray.length -1; i+=2, j++){
                newArray[j] = currentArray[i] + currentArray[i+1];
            }
    
            return newArray;
        }
    
        public static void main(String[] args){
            int[] arrayANew;
            int[] arrayA = {5,8,4,2};
    
            arrayANew= reduce(arrayA);
    
            System.out.println(Arrays.toString(arrayANew) );
        }
    }
    

    【讨论】:

      【解决方案3】:

      每次通过 for 循环时,您需要将您在 currentArray 中访问的索引移动两个:

      public static int[] reduce(int[] currentArray) {
          int[] newArray = new int[currentArray.length / 2]; //needs to be half the length
      
          for(int i = 0; i < newArray.length; i++) { //use newArray's length since we are iterating over newArray
              newArray[i] = currentArray[2 * i] + currentArray[2 * i + 1]; //need to multiply indices by 2 to provide that "jumping" effect
          }
      
          return newArray;
      }
      
      public static void main(String[] args) {
          int[] arrayANew;
          int[] arrayA = { 5, 8, 4, 2 };
          arrayANew = reduce(arrayA);
          System.out.println(Arrays.toString(arrayANew));
      }
      

      请注意,这假定数组具有偶数个元素。如果您还需要它来处理奇数长度的数组,请使用以下命令:

      public static int[] reduce(int[] currentArray) {
          int[] newArray = new int[currentArray.length / 2 + ((currentArray.length & 1) == 1 ? 1 : 0)]; //needs to be half the length
      
          for(int i = 0; i < newArray.length; i++) { //use newArray's length since we are iterating over newArray
              newArray[i] = currentArray[2 * i] + (2 * i + 1 >= currentArray.length ? 0 : currentArray[2 * i + 1]); //need to multiply indices by 2 to provide that "jumping" effect
          }
      
          return newArray;
      }
      
      public static void main(String[] args) {
          int[] arrayANew;
          int[] arrayA = { 5, 8, 4, 2 };
          arrayANew = reduce(arrayA);
          System.out.println(Arrays.toString(arrayANew));
      }
      

      【讨论】:

      • 我已经更新了我的答案,还包括一个适用于具有奇数个元素的数组的解决方案。
      【解决方案4】:

      这将处理偶数或奇数长度的数组。如果奇数,result 数组中的最后一个元素将是vals 数组中的最后一个元素。

      int[] vals = { 10, 2, 3, 10, 1, 2, 3, 6};
      int[] result = new int[(vals.length +1) / 2];
      for (int i = 0; i < vals.length; i += 2) {
          result[i / 2] = vals[i] + (i <vals.length-1 ? vals[i + 1] : 0);
      }
      
      System.out.println(Arrays.toString(result));
      

      打印

      [12, 13, 3, 9]
      

      需要考虑的要点。

      • results 数组的大小可以根据需要进行四舍五入。
      • 是否尝试添加最后一个值由ternary 运算符控制

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-15
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        • 2021-06-21
        • 2020-07-14
        • 1970-01-01
        相关资源
        最近更新 更多