简体   繁体   English

java方法中的数组,执行什么操作?

[英]array in java methods, what is executed?

I have been struggling with this java problem for several days now, and now I have to give up. 我已经为这个Java问题苦苦挣扎了好几天了,现在我不得不放弃了。 I have been told the answer which should be 5 5 3 3, but I cannot in any way see how it is possible to get that result. 有人告诉我答案应该是5 5 3 3,但是我无法以任何方式看到如何获得该结果。

Given the following java method: 给定以下java方法:

public int[] methodName(int[] nums)
{
     int largestOdd=0;
     for(int i=nums.length-2;i>=0;i--)
     {
          if (nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)
               largestOdd = nums[i+1];

          if (nums[i] == 0)
               nums[i] = largestOdd;
     }
     return(nums);
}

What is printed when the following Java statements are executed? 执行以下Java语句时显示什么?

int[] nums = {0,5,0,3};
nums = methodName(nums)
for (int i = 0; i<nums.length;i++)
    System.out.print(nums[i] + "");
System.out.println();

It just doesnt make any sense for me that first of all it will start printing "5". 对我而言,它首先将开始打印“ 5”是没有任何意义的。 In my opinion it should be "3" because nums[2+1] = 3 (last index element) 我认为应该为“ 3”,因为nums [2 + 1] = 3(最后一个索引元素)

Second of all why will it print four numbers when the loop in the method only will loop through 3 times until hitting -1 ? 第二,当方法中的循环仅循环3次直到达到-1时,为什么还要打印四个数字?

If someone can explain how to get the result in a understandable way, I would be very happy. 如果有人能够以一种可以理解的方式解释如何获得结果,我将非常高兴。

Thanks in advance 提前致谢

methodName runs backwards through the array, examining every pair of numbers. methodName向后遍历数组,检查每对数字。 In this case, the first pair examined will be (0,3). 在这种情况下,检查的第一对为(0,3)。

As it runs over the pairs, methodName keeps track of the largest odd number seen (it looks at the second number of each pair for this). 当它遍历对时, methodName会跟踪看到的最大奇数(为此,它查看每对中的第二个数字)。

Whenever the first number is zero, it sets it to the largest odd number seen so far. 只要第一个数字为零,就会将其设置为迄今为止看到的最大奇数。

So in this case, it will: 因此,在这种情况下,它将:

  1. Look at (0,3). 看(0,3)。
  2. Is 3 odd? 3是奇数吗? Yes. 是。 Is it the largest odd number seen so far? 是迄今为止看到的最大的奇数吗? Yes. 是。 So keep track of 3. 因此,请跟踪3。
  3. Is 0 zero? 0是零吗? Yes. 是。 So set it to 3. Now the array is {0, 5, 3, 3}. 因此将其设置为3。现在数组为{0,5,3,3}。
  4. Move our indices back by 1 and look at (5, 3). 将索引移回1,然后看(5,3)。
  5. Is 3 odd? 3是奇数吗? Yes. 是。 Is it the largest odd number seen so far? 是迄今为止看到的最大的奇数吗? No. 没有。
  6. Is 5 zero? 5是零吗? No. 没有。
  7. Move our indices back by 1 and look at (0, 5). 将索引移回1,然后看(0,5)。
  8. Is 5 odd? 5是奇数吗? Yes. 是。 Is it the largest odd number seen so far? 是迄今为止看到的最大的奇数吗? Yes. 是。 So keep track of 5. 因此,请注意5。
  9. Is 0 zero? 0是零吗? Yes. 是。 So set it to 5. Now the array is {5, 5, 3, 3}. 因此将其设置为5。现在数组为{5,5,3,3}。
  10. We are already at the beginning of the array, so we can't go back any further. 我们已经在数组的开头,所以我们不能再回头了。
  11. Return to the main method, and print the contents of the array. 返回main方法,并打印数组的内容。
{0,5,0,3}

for(int i=nums.length-2;i>=0;i--) // starts at 0 (index 2) <-> num.length 4 - 2

   // runs 3 times  num.length(4) - 2 = 0

   if (nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)
           largestOdd = nums[i+1]; <-->  // 3

           // if odd and grater than 0 
           // first iteration largestOdd = 3


   if (nums[i] == 0)   // still i is index 2 {0, 5, 0, 3} = 0, so true
          nums[i] = largestOdd;  // nums[i] (index 2) = 3

          // after first iteration
          {0, 5, 3, 3}


  Second iteration do nothing (0 is not odd)

  Third iteration, do same as first iteration, making final array
  {5, 5, 3, 3}

Next Part 下一部分

 // This method returns the same array you passed into to
 public int[] methodName(int[] nums)
 {
     return(nums);
  }

 // So
 nums = methodName(nums) = {5, 5, 3, 3} The new array produced by the method
you can analyse step by step.
In the for-loop: i starts from 2 to 0;
num = [0,5,0,3]
1.
    i = 2; largestOdd = 0;   nums[i+1] = nums[3] = 3;   nums[i] = nums[2] = 0;
    first condition "(nums[i+1] % 2 != 0 && nums[i+1] > largestOdd)" is true
        largestOdd = nums[i+1] = 3
    second condition (nums[i] == 0) is true
        nums[i] = nums[2] = largestOdd = 3
    nums = [0,5,3,3];
 2.
    i=1;   largestOdd = 3;   nums[i+1] = nums[2] = 3;   nums[i] = nums[1] = 5;
    first condition is false;
    second condition is false;
    nums = [0,5,3,3];
 3.
    i=0;  largestOdd = 3;   nums[i+1] = nums[1] = 5;   nums[i] = nums[0] = 0;
    first condition is true
         largestOdd = 5;
    second condition nums[0] = 0    is true
         nums[0] = largestOdd = 5;
    nums = [5,5,3,3];
Loop 1 start i=2,nums=[0,5,0,3],largestOdd=0
because:(nums[2+1]=3)/2 !=0 && (nums[2+1]=3)>(largestOdd=0)
so:largestOdd=(nums[2+1]=3)=3
beacuse:(nums[2]=0) ==0
so:nums[2]=(largestOdd=3)=3
Loop 1 end   i=2,nums=[0,5,3,3],largestOdd=3

Loop 2 start i=1,nums=[0,5,3,3],largestOdd=3
because:(nums[1+1]=3)/2 !=0 && (nums[1+1]=3)>(largestOdd=3)
so:next
beacuse:(nums[1]=5) !=0
so:next
Loop 2 end   i=1,nums=[0,5,3,3],largestOdd=3

Loop 3 start i=0,nums=[0,5,3,3],largestOdd=3
because:(nums[0+1]=5)/2 !=0 && (nums[0+1]=5)>(largestOdd=3)
so:largestOdd=(nums[0+1]=5)=5
beacuse:(nums[0]=0) ==0
so:nums[0]=(largestOdd=5)=5
Loop 3 end   i=0,nums=[5,5,3,3],largestOdd=5

END LOOP

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

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