当我在java中传递一个字符数组时,传递的数组是不同的。例如,如果我传递一个数组aqdf,那么传递的数组实际上是aqqq

当我在java中传递一个字符数组时,传递的数组是不同的。例如,如果我传递一个数组aqdf,那么传递的数组实际上是aqqq,java,arrays,pass-by-reference,Java,Arrays,Pass By Reference,我正在尝试实现一种算法,以确定字符串是否具有所有唯一字符,而不使用任何额外的DataStruments。 这是我的密码: package CTCI; import java.util.Scanner; public class ArrStrng1 { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter

我正在尝试实现一种算法,以确定字符串是否具有所有唯一字符,而不使用任何额外的DataStruments。 这是我的密码:

package CTCI;

import java.util.Scanner;

public class ArrStrng1 {

    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter the number of characters in the string?");
        int l=s.nextInt();
        char[] ca=new char[l];
        System.out.println("Enter the characters in the string?");
        for(int i=0;i<l;i++)
        {
            char c=s.next().charAt(0);  
            ca[i]=c;

        }

        if(unique(ca,l))
            System.out.println("YES");
        else
            System.out.println("NO");
      s.close();    

    }

    public static boolean unique(char[] str,int l)
    {
        //using insertion sort to sort the character array
        char c;
        for(int i=1;i<l;i++)
        {
             c=str[i];
             int j=i;
             while(j>0 && c<str[j-1])
             {
                 str[j]=str[j-1];
                 j--;

             }


        }
        //Now checking if any two consecutive characters are same
        for(int j=0;j<l-1;j++)
        {
            if(str[j]==str[j+1])
                return false;
        }
        return true;//If no two consecutive characters are same then the character array is unique
    }

}
package-CTCI;
导入java.util.Scanner;
公共类ArrStrng1{
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入字符串中的字符数?”);
int l=s.nextInt();
char[]ca=新字符[l];
System.out.println(“在字符串中输入字符?”);

对于(int i=0;i插入排序有问题,您忘记在较小的索引(j-1)中插入c的值。因此正确的排序是:

    char c;  
    for(int i=1;i<l;i++)
    {
         c=str[i];
         int j=i;
         while(j>0 && c<str[j-1])
         {                 
             str[j]=str[j-1];
             str[j-1]=c;
             --j;
         }
    }  
charc;

对于(inti=1;i0&&c我假设您希望自己实现插入排序。 因为java有可以在数组上使用的排序。 在插入排序实现中,无论以何种方式出现问题,您都忘记在数字之间切换,只需分配较大的字符,请尝试以下操作:

 char c;
        char tmp;
        for(int i=1;i<l;i++)
        {
             c=str[i];
             int j=i;
             while(j>0 && c<str[j-1])
             {
                 tmp = str[j];
                 str[j]=str[j-1];
                 str[j-1] = tmp;
                 j--;

             }


        }
//      Arrays.sort(str);
        //Now checking if any two consecutive characters are same
charc;
char-tmp;

对于(int i=1;i0&&c在数组中更改内容的唯一位置是排序。将代码放入调试器中,然后找到代码在其中指定了一个您不期望的值。我所看到的是,在我愿意花费大量时间的情况下,您将一个字符指定给“c”,但不要将其放回数组中的任何位置。请尝试打印con我使用了调试器,发现数组char[]ca输入正确,但是当它被传递到函数unique(char[]str,int l)时它会被修改。所以请在unique中调试它--让我们知道您尝试了哪些不理解的内容。不要要求我们为您调试它。我不明白在传递给unique函数时为什么要修改字符数组?