简体   繁体   English

为什么下面的代码没有返回所需的 output? 它应该返回 1,3,2,2,0,0,0 而它返回相同的数组

[英]Why this below code is not returning the desired output ? it should return 1,3,2,2,0,0,0 while its returning same array

When I run the below code it's returning same array as output.当我运行下面的代码时,它返回与 output 相同的数组。 Can anyone tell me where I am wrong?谁能告诉我我错在哪里?

public class MoveZeroToend {
    public static void main(String[] args) {
        int[] arr = { 1, 3, 0, 2, 0, 2, 0 };
        Move0Toend(arr);
    }

    static void Move0Toend(int[] arr) { // Code to move zeroes to end
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                swap(arr[i], arr[count]);
                count++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " "); // Print the array
        } 
    }

    static void swap(int a, int b) { // To swap
        a = a + b;
        b = a - b;
        a = a - b;
    }
}

on your swap method, you are not swapping the actual values of the objects you've passed, you are swapping between the values passed to the method but there is no result returned so nothing happens.在您的交换方法上,您并没有交换您传递的对象的实际值,而是在传递给该方法的值之间交换,但没有返回结果,因此没有任何反应。 you need to either do the swap on the actual objects - not in a method, or use another way for this.您需要对实际对象进行交换 - 而不是在方法中,或者为此使用另一种方式。 I would recommend googling "pass by value" and "pass by reference".我建议使用谷歌搜索“按值传递”和“按引用传递”。 I would also recommend adding a unit test or at least debug the program so you can validate your code is doing what you want.我还建议添加一个单元测试或至少调试程序,以便您可以验证您的代码正在执行您想要的操作。

Your swap method doesn't return any values, nor does it change the values in the reference of the objects passed.您的 swap 方法不返回任何值,也不会更改传递的对象的引用中的值。 To fix this you can either return two values from your swap method (a and b) or you could do it not in a method, that way it would directly affect the objects.要解决此问题,您可以从交换方法(a 和 b)返回两个值,也可以不在方法中执行此操作,这样它会直接影响对象。 Just for a little more explanation, the variables a and b in your swap method are local to the swap method, changing these would not affect any other variables, even if they were also named the same, and as your method is a void it can't return anything.只是为了多解释一点,您的 swap 方法中的变量 a 和 b 是 swap 方法的本地变量,更改它们不会影响任何其他变量,即使它们的名称也相同,并且由于您的方法是 void 它可以不返回任何东西。 Hope this helps:)希望这可以帮助:)

Your swap() method isn't performing any operation on your array, you are just passing two values a and b and swapping them but no operation is being performed on your array.您的 swap() 方法没有对您的数组执行任何操作,您只是传递两个值 a 和 b 并交换它们,但没有对您的数组执行任何操作。

Instead of passing these two values to your swap() method you can directly swap them inside your for loop as below:您可以直接在 for 循环中交换它们,而不是将这两个值传递给您的 swap() 方法,如下所示:

for(int i=0;i<arr.length;i++){
        if(arr[i]!=0){
            int temp = arr[i];
            arr[i] = arr[count];
            arr[count] = temp;
            count++;
        }
    }

暂无
暂无

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

相关问题 为什么下面的代码返回设置大小为5? 即使覆盖了hashcode和equals方法,它也应将设置大小返回为3 - why below code returning set size as 5? even though hashcode and equals method is overridden it should return set size as 3 任何人都可以看到我错在哪里,我的代码应该返回 true 但它返回 false 它不应该返回 - Can anyone see where i am wrong , My code should return true but its returning false which it should not be returning 为什么Pattern.matches(“ [a * mn]”,“ aaaa”)返回true? 什么是适当的代码才能获得所需的输出? - Why doesn't Pattern.matches(“[a*mn]”,“aaaa”) return true? What should be proper code to get the desired output? 当数组超出所需范围时返回null - returning null when the array is outside the desired range matcher.find()在返回true时返回false - matcher.find() returning false while it should return true 为什么在一个应该返回简短工作的方法中返回一个int? - Why does returning an int in a method that should return a short work? 我在这段代码中返回一个字符串数组,但仍然说此方法必须返回String类型的结果。 为什么? - I am returning an array of string in this code but still it says this method must return a result of type String. why? Java程序输出应为true,但其返回false为何? - Java program output should be true , But its return false why? 为什么 String Builder 返回其长度为零,同时返回它的一些值 - Why String Builder is returning its length zero, while returning some value of it 为什么此代码不返回字符串? - Why is this code not returning a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM