英文:
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<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));
}
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= {"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));
}
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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论