【问题标题】:java | Return a reversed array by FOR loop爪哇 |通过 FOR 循环返回一个反转数组
【发布时间】:2018-05-20 17:37:35
【问题描述】:

我遇到了一个练习,它告诉我从给定的数组创建一个反向数组。

经过一番思考,我编写了这样的代码:

    public int[] reverse3(int[] nums) {

  int[] nums2 = new int[3];

  for (int i = nums.length - 1; i >= 0; i--) {

     for (int j = 0; j < nums.length; j++) {

          nums2[j] = nums[i];

    }
  }
  return nums2;
}

但它抛出了三个完全相同的数字。

【问题讨论】:

  • 提示:你只需要一个循环。

标签: java arrays loops for-loop reverse


【解决方案1】:

您不需要嵌套的 for 循环 - 只需遍历源数组并以相反的顺序填充结果数组:

public int[] reverse(int[] nums) {
    int len = nums.length;
    int[] result = new int[len];
    for (int i = 0; i < len; ++i) {
        result[len - i - 1] = nums[i];
    }
}

【讨论】:

    【解决方案2】:

    乍一看,您的代码应该更像这样:

    public int[] reverse3(int[] nums) 
    {
    
      // initialize a second array with the same length
      int[] nums2 = new int[nums.length];
      // initialize the nums2 index
      int index = 0;
    
      // you only need one loop for this (since we'll be incrementing the index of nums2)
      for (int i = nums.length - 1; i >= 0; i--) {
         nums2[index] = nums[i];
         index++;
      }
    
      return nums2;
    }
    

    【讨论】:

      【解决方案3】:

      像这样交换数组中的对称值:

       public static void reverse(int[] nums) {
          for (int i = 0; i < nums.length / 2; i++) {
              int temp = nums[i];
              nums[i] = nums[nums.length - 1 - i];
              nums[nums.length - 1 - i] = temp;
          }
      

      【讨论】:

        【解决方案4】:

        要反转数组,您只需交换元素直到中点:

        public int[] reverse(int[] nums) {
            int numsLength = nums.length;
            for (int i = 0; i < numsLength / 2; i++) {
                int temp = nums[i];
                nums[i] = nums[numsLength - i - 1];
                nums[numsLength - i - 1] = temp;
            }
            return nums;
        }
        

        这种方式更加优化。

        Source: How do I reverse an int array in Java?

        【讨论】:

          【解决方案5】:

          我是 Java 开发的新手,如果这个问题也很抱歉,我很抱歉 很傻,但似乎我被这个告诉我的练习所困 从给定的数组创建一个反向数组。

          经过一番思考,我编写了这样的代码:

              public int[] reverse3(int[] nums) {
          
            int[] nums2 = new int[3];
          
            for (int i = nums.length - 1; i >= 0; i--) {
          
               for (int j = 0; j < nums.length; j++) {
          
                    nums2[j] = nums[i];
          
              }   }   return nums2; }
          

          但它抛出了三个完全相同的数字。我能数数吗 在一些帮助?谢谢

          只使用一个循环。如果您想使用 2 个数组(我看不到重点。)这将起作用:

              int j = 0;
          
              for(int i = nums.length -1; i >= 0; i--){
                   nums2[j] = nums[i];
                   j++;
               }
          

          但如果你只想使用一个数组,你可以这样做:

          for (int i = 0; i < nums.length/2; i++) {
              int aux = nums[i];
              nums[i] = nums[nums.length-i-1];
              nums[nums.length-i-1] = aux;
          }
          

          【讨论】:

            【解决方案6】:

            有很多有效的方法可以做到这一点,但为了让你明白我会修改你自己的代码

            public int[] reverse3(int[] nums) {

                  int[] nums2 = new int[3];
            
                  for (int i = nums.length - 1; i >= 0; i--) {
            
                     for (int j = (nums.length-1) - i; j < nums.length; j++) {
            
                          nums2[j] = nums[i];
            
                    }
                  }
            
                  return nums2;
                }
            

            或者让我们做一些修改而不是一次又一次地使用 nums.length() 我们可以把它放在一个变量中

            public int[] reverse3(int[] nums) {

                  int[] nums2 = new int[3];
                  int length = nums.length;
                  for (int i = length - 1; i >= 0; i--) {
            
                     for (int j = (length-1) - i; j < length; j++) {
            
                          nums2[j] = nums[i];
            
                    }
                  }
            
                  return nums2;
                }
            

            请记住,这不是一种有效的方法,但为了让您理解,我只是修改了逻辑。使用这样的嵌套循环会降低性能,所以最好避免它并尝试以更优化的方式来做......

            【讨论】:

              猜你喜欢
              • 2016-01-14
              • 2020-10-05
              • 2018-05-24
              • 2020-06-04
              • 1970-01-01
              • 2015-07-20
              • 2021-04-24
              • 2018-09-02
              • 2014-08-11
              相关资源
              最近更新 更多