简体   繁体   English

使用charAt()更改char数组

[英]Using charAt() to change a char array

I'm utterly boggled as to why charAt() works in some scenarios but not others. 我完全不知道为什么charAt()在某些情况下工作但在其他情况下不工作。 I am doing exercises while learning Java and one of them was to take a string, and return it in reverse order. 我在学习Java时正在练习,其中一个是接受一个字符串,然后以相反的顺序返回它。

My working code: 我的工作代码:

public String reverseString(String tempStr){
    int initialindex = tempStr.length()-1;
    int reverseindex = 0;
    char tmp;
    char[] array = new char[tempStr.length()];

    for(int tempchar : array){
        tmp = tempStr.charAt(initialindex);
        array[reverseindex] = tmp;
        initialindex--;
        reverseindex++;
    }
    String returnstr = new String(array);
    return returnstr;
}

The problem I ran into is using the following for statement prints gibberish: 我跑进使用以下问题for语句打印乱码:

for(int tempchar : array){
    array[reverseindex] = tempStr.charAt(initialindex);
    initialindex--;
    reverseindex++;
}

There were perhaps a dozen different variants of using while loops, standard for loops and a few other versions of code that were ugly and didn't work. 有可能使用的十几个不同的变种while循环,标准for循环和代码是丑陋,没有工作的几个其他版本。 Why did my making a char tmp field, putting the inspected characrer in said field, and then using said field to enter the data into an array work? 为什么我制作一个char tmp字段,将检查过的字符放在所述字段中,然后使用所述字段将数据输入到数组工作中?

Also, why am I unable to just return the string using return array.toString(); 另外,为什么我不能使用return array.toString();返回字符串return array.toString(); ?

Edit: I'm using the latest Eclipse I downloaded today, switched from netbeans. 编辑:我正在使用我今天下载的最新Eclipse,从netbeans切换。

  1. I copied your code into my editor and it performed fine using either version, with tmp field or without. 我将您的代码复制到我的编辑器中,使用tmp字段或不使用任何版本都可以正常运行。 You must have made some other error using the other method. 您必须使用其他方法发生其他错误。

  2. Java doesn't support pretty .toString() for arrays; Java不支持数组的漂亮.toString() ; any object which does not override toString will produce the hashCode of the object rather than the contents/fields of the object, and arrays are no exception here. 任何不覆盖toString的对象都会产生对象的hashCode而不是对象的contents / fields,而数组在这里也不例外。 Whilst it might seem sensible for character arrays, the same operation on an int array would produce nonsense; 虽然对于字符数组似乎很合理,但对int数组的相同操作会产生无意义的操作; See the difference between Arrays.toString() and String.valueOf(array) . 查看Arrays.toString()String.valueOf(array)之间的区别。 In this case, you probably want to use the String.valueOf method. 在这种情况下,您可能希望使用String.valueOf方法。

As a hint to get you started: if you want to convert a char array into a String use the String constructor that takes a char array. 作为一个提示让你开始:如果你想转换一个char数组转换成一个String使用String构造函数,一个char数组。

Update: I see you already did that in your edit. 更新:我看到你在编辑中已经这样做了。 Does it work as expected now? 它现在按预期工作吗?

Your loop looks a little bit weird since you never use your loop variable. 你的循环看起来有点奇怪,因为你从不使用你的循环变量。 you could try this: 你可以试试这个:

char[] initialArray = initialStr.toCharArray();
char[] array = new char[tempStr.length()];

for(int srcIndex = 0, destIndex = array.length-1; destIndex >= 0; srcIndex++, destIndex--) {
    array[destIndex] = initialArray[srcIndex];
}

The array.toString() return string representation of the object. array.toString()返回对象的字符串表示形式。 You need to use char[] constructor of String new String(array) to create String from the char[]. 您需要使用String new String(array) char []构造函数从char []创建String。

   public String reverse(String str)
        {
            if(str == null)
            {
               return null;
             }
           byte[] byteArray=  str.getBytes();
           int arrayLastIndex = byteArray.length -1 ;

           for(int i=0 ; i < byteArray.lenght/2: i++)
           {
              byte temp = byteArray[i];
              byteArray[i] = byteArray[arrayLastIndex -i ]
              byteArray[arrayLastIndex - i] = temp;
           }

           return new String(byteArray);
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM