下面的代码总是无缘无故地返回 false,而事实上它不应该这样。

huangapple go评论81阅读模式
英文:

The next code always returns false for some reason when it shouldn't

问题

public class Main {

    public static void main(String[] args) {

        int arr[] = {1, 2, 3, 1, 2, 3};

        System.out.println(sda(arr));

    }//main

    public static boolean sda(int[] arr) {

        int[] arr2 = new int[arr.length];
        int[] arr3 = new int[arr.length];

        for(int i = 0; i < (arr2.length/2); i++) {
            arr2[i] = arr[i];
        }
        for(int i = arr.length - 1; i > (arr.length/2) - 1; i--) {
            arr3[i] = arr[i];
        }

        for(int j = 0; j < arr.length/2 - 1; j++) {
            if(arr3[j] != arr2[j]) return false;
        }
        return true;
    }
}
英文:

supposed to return true if the first half of an array is equal to the second but it keeps return false... I tried debugging but still doesn't work... Help!

public class Main {

	public static void main(String[] args) {

		int arr[] = {1, 2, 3, 1, 2, 3};

		System.out.println(sda(arr));

	}//main

	public static boolean sda(int[] arr) {

		int[] arr2 = new int[arr.length];
		int[] arr3 = new int[arr.length];

		for(int i = 0; i &lt; (arr2.length/2); i++) {
			arr2[i] = arr[i];
		}
		for(int i = arr.length - 1; i &gt; (arr.length/2) - 1; i--) {
			arr3[i] = arr[i];
		}

		for(int j = 0; j &lt; arr.length/2 - 1; j++) {
			if(arr3[j] != arr2[j]) return false;
		}
		return true;
	} 
}

any suggestions?

答案1

得分: 3

Emmm... 嗯,一个好的解决方法是在 <pre>arr2</pre> 和 <pre>arr3</pre> 的值上进行调试。然后你会发现这样:

int arr2[] = {1, 2, 3, 0, 0, 0};
int arr3[] = {0, 0, 0, 1, 2, 3};

加油!去做并解决它。

英文:

Emmm... Well, a good resolve method is to debug on the value of <pre>arr2</pre> and <pre>arr3</pre>. Then you'll find this:

int arr2[] = {1, 2, 3, 0, 0, 0};
int arr3[] = {0, 0, 0, 1, 2, 3};

Come on! Do it and resolve it.

答案2

得分: 0

如果我分解当前的代码:
首先,arr2 和 arr3 被赋值为空数组,各包含 6 个项。
接下来,arr2 在数组的前 3 个元素中被赋值为 (1,2,3)。
然后,arr3 在数组的最后 3 个元素中被赋值为 (1,2,3)。
如果我将下面的代码片段添加到第二个 for 循环之下:

System.out.println("arr2:");
for(int i : arr2){
    System.out.println(i);
}

System.out.println("arr3:");
for(int i : arr3){
    System.out.println(i);
}

你会看到这两个数组是不同的。

arr2:
1
2
3
0
0
0
arr3:
0
0
0
1
2
3

要使结果返回 true,你可以调整 arr2 的值放置位置,或者调整 arr3 的值放置位置在数组中。
另一种解决方案是将最终的 for 循环中的两个数组值的比较改为:

for(int j = 0; j < arr.length/2 - 1; j++) {
    if(arr3[j + (arr.length/2)] != arr2[j]) return false;
}

这样,这两个循环的比较将针对 arr2 的前 3 个值与 arr3 的最后 3 个值。
总之,我已将 sda 方法更改如下:

public static boolean sda(int[] arr) {

    int[] arr2 = new int[arr.length];
    int[] arr3 = new int[arr.length];

    for(int i = 0; i < (arr2.length/2); i++) {
        arr2[i] = arr[i];
    }
    for(int i = arr.length - 1; i > (arr.length/2) - 1; i--) {
        arr3[i] = arr[i];
    }

    for(int j = 0; j < arr.length/2 - 1; j++) {
        if(arr3[j + (arr.length/2)] != arr2[j]) return false;
    }
    return true;
}
英文:

If I break down the current code:
First arr2 and arr3 are assigned an empty array of 6 items.
Next arr2 is assigned values (1,2,3) in the first 3 elements of the array.
Then arr3 is assigned (1,2,3) in the final 3 elements of the array.
If I add the below code snipper under the 2nd for-loop:

        System.out.println(&quot;arr2: &quot;);
        for(int i : arr2){
            System.out.println(i);
        }

        System.out.println(&quot;arr3: &quot;);
        for(int i : arr3){
            System.out.println(i);
        }

You will see that that the two arays are different.

arr2: 
1
2
3
0
0
0
arr3: 
0
0
0
1
2
3

To make the result return true, you could adjust where the arr2 values are placed or adjust where the arr3 values are placed within the array.
An alternative solution would be to change the evaluation of the two array values in the final for-loop to be:

for(int j = 0; j &lt; arr.length/2 - 1; j++) {
            if(arr3[j + (arr.length/2)] != arr2[j]) return false;
        }

This way the evaluation of the two loops will be for the first 3 values of arr2 against the final 3 values of arr3.
In summary, I have changed the sda method to below:

  public static boolean sda(int[] arr) {

        int[] arr2 = new int[arr.length];
        int[] arr3 = new int[arr.length];

        for(int i = 0; i &lt; (arr2.length/2); i++) {
            arr2[i] = arr[i];
        }
        for(int i = arr.length - 1; i &gt; (arr.length/2) - 1; i--) {
            arr3[i] = arr[i];
        }
        
        for(int j = 0; j &lt; arr.length/2 - 1; j++) {
            if(arr3[j + (arr.length/2)] != arr2[j]) return false;
        }
        return true;
    }

答案3

得分: -1

public static boolean sda(int[] arr) {

    for(int i=0, j=arr.length/2; i<arr.length/2 || j<arr.length; i++, j++)
        if(arr[i]!=arr[j])
            return false;
    return true;
}
英文:
public static boolean sda(int[] arr) {
	
	for(int i=0, j=arr.length/2; i&lt;arr.length/2 || j&lt;arr.length; i++, j++)
		if(arr[i]!=arr[j])
			return false;
    return true;
}

huangapple
  • 本文由 发表于 2020年8月22日 13:22:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63532835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定