简体   繁体   中英

Using charAt() to change a char array

I'm utterly boggled as to why charAt() works in some scenarios but not others. I am doing exercises while learning Java and one of them was to take a string, and return it in reverse order.

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(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. 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?

Also, why am I unable to just return the string using return array.toString(); ?

Edit: I'm using the latest Eclipse I downloaded today, switched from netbeans.

  1. I copied your code into my editor and it performed fine using either version, with tmp field or without. You must have made some other error using the other method.

  2. Java doesn't support pretty .toString() for arrays; 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. Whilst it might seem sensible for character arrays, the same operation on an int array would produce nonsense; See the difference between Arrays.toString() and String.valueOf(array) . In this case, you probably want to use the String.valueOf method.

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.

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. You need to use char[] constructor of String new String(array) to create String from the char[].

   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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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