函数在多次使用时未返回所期望的值。

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

Function didn't return the desired value when it is used more than once

问题

以下是翻译好的内容:

当我使用sortAsc_byPosition函数两次时

public class stringArraySort {
	public static String[] sortAsc_byPosition(String[] str, int charPosition) {
		for(int r=0; r<str.length-1; r++) {
			for(int i=r+1; i<str.length; i++) {
				int j=charPosition;
				while (str[r].charAt(j)==str[i].charAt(j)) {
					j++;
				}
				if (str[r].charAt(j)>str[i].charAt(j)) {
					String tmp=str[i];
					str[i]=str[r];
					str[r]=tmp;
				}
			}
		}
		return str;
	}

	public static void main(String[] args) {
		String[] productID= {"Xcra-S1836", "Zkda-D3426", "Ypdu-B5654", "Akdr-Z5075", "Jhbd-K4051"};
		String[] result1=sortAsc_byPosition(productID, 5);
		String[] result2=sortAsc_byPosition(productID, 6);
		
		System.out.println("Result of array sorted by character position at 5 :" + Arrays.toString(result1));
		System.out.println("Result of array sorted by character position at 6 :" + Arrays.toString(result2));
	}

以获取所需结果,该函数未能成功实现。

但是,当我仅使用函数一次时,它可以正常工作。

public static void main(String[] args) {
		String[] productID= {"Xcra-S1836", "Zkda-D3426", "Ypdu-B5654", "Akdr-Z5075", "Jhbd-K4051"};
		String[] result1=sortAsc_byPosition(productID, 5);
		
		System.out.println("Result of array sorted by character position at 5 :" + Arrays.toString(result1));
	}

这个问题的主要原因是什么?正确的解决方法是什么?谢谢您的回答...

英文:

When I used the sortAsc_byPosition function twice

public class stringArraySort {
	public static String[] sortAsc_byPosition(String[] str, int charPosition) {
		for(int r=0; r&lt;str.length-1; r++) {
			for(int i=r+1; i&lt;str.length; i++) {
				int j=charPosition;
				while (str[r].charAt(j)==str[i].charAt(j)) {
					j++;
				}
				if (str[r].charAt(j)&gt;str[i].charAt(j)) {
					String tmp=str[i];
					str[i]=str[r];
					str[r]=tmp;
				}
			}
		}
		return str;
	}

	public static void main(String[] args) {
		String[] productID= {&quot;Xcra-S1836&quot;, &quot;Zkda-D3426&quot;, &quot;Ypdu-B5654&quot;, &quot;Akdr-Z5075&quot;, &quot;Jhbd-K4051&quot;};
		String[] result1=sortAsc_byPosition(productID, 5);
		String[] result2=sortAsc_byPosition(productID, 6);
		
		System.out.println(&quot;Result of array sorted by character position at 5 :&quot; + Arrays.toString(result1));
		System.out.println(&quot;Result of array sorted by character position at 6 :&quot; + Arrays.toString(result2));
	}

to get the desired result, the function failed to make it.

But, when I used the function only once, it works.

public static void main(String[] args) {
		String[] productID= {&quot;Xcra-S1836&quot;, &quot;Zkda-D3426&quot;, &quot;Ypdu-B5654&quot;, &quot;Akdr-Z5075&quot;, &quot;Jhbd-K4051&quot;};
		String[] result1=sortAsc_byPosition(productID, 5);
		
		System.out.println(&quot;Result of array sorted by character position at 5 :&quot; + Arrays.toString(result1));
	}

What is the main cause of this problem? What is the correct way to make it? Thanks for the answer...

答案1

得分: 3

你的sortAsc_byPosition方法接受一个数组,对该数组进行更改,并返回相同的数组。这意味着result1 == result2(两个变量都是指向同一数组对象的引用)。

因此,当您连续两次执行该方法时,最终看到的结果将是第二次调用所使用的排序顺序。

如果您希望拥有两个不同顺序的数组,您应该返回数组的副本。

例如:

public static String[] sortAsc_byPosition(String[] str, int charPosition) {
    String[] result = Arrays.copyOf(str, str.length);
    for (int r = 0; r < result.length - 1; r++) {
        for (int i = r + 1; i < result.length; i++) {
            int j = charPosition;
            while (result[r].charAt(j) == result[i].charAt(j)) {
                j++;
            }
            if (result[r].charAt(j) > result[i].charAt(j)) {
                String tmp = result[i];
                result[i] = result[r];
                result[r] = tmp;
            }
        }
    }
    return result;
}
英文:

Your sortAsc_byPosition method accepts an array, makes changes to that array and returns the same array. This means result1 == result2 (both variables are references to the same array object).

Therefore, when you execute the method twice, the result you'll see in the end is the sort order used by the second call.

You should return a copy of the array if you want to have two different arrays having different order.

For example:

public static String[] sortAsc_byPosition(String[] str, int charPosition) {
    String[] result = Arrays.copy(str,str.length);
    for(int r=0; r&lt;result.length-1; r++) {
        for(int i=r+1; i&lt;result.length; i++) {
            int j=charPosition;
            while (result[r].charAt(j)==result[i].charAt(j)) {
                j++;
            }
            if (result[r].charAt(j)&gt;result[i].charAt(j)) {
                String tmp=result[i];
                result[i]=result[r];
                result[r]=tmp;
            }
        }
    }
    return result;
}

huangapple
  • 本文由 发表于 2020年7月26日 18:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098946.html
匿名

发表评论

匿名网友

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

确定