How can I get a char array in reverse order?

2020-03-07 11:49发布

my assignment question is like that

Write a program which prints the letters in a char array in reverse order using

   void printReverse(char letters[], int size);

For example, if the array contains {'c', 's', 'c', '2', '6', '1'} the output should be "162csc".

I tried, but I don't know what it means

void printReverse(char letters[], int size);

I did this but there's problem with calling the method "printReverse" into main method

import java.util.Arrays;
import java.util.Collections;

public class search {

public static void main(String[] args) {          

char[] letters = {'e', 'v', 'o', 'l', '4'};
printReverse();

}

public void printReverse(char[] letters, int size){

for (int i = letters.length-1; i >= 0 ; i--){
System.out.print(letters[i]);
}
}

}

标签: java arrays char
8条回答
Evening l夕情丶
2楼-- · 2020-03-07 12:25

I believe what you wrote is the signature of the method you have to create.

public void printReverse(char[] letters, int size){
   //code here
}

You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:

for (int i = array.length-1; i >= 0 ; i--){
    System.out.print(array[i]);
}
查看更多
姐就是有狂的资本
3楼-- · 2020-03-07 12:28

This should take about 6 ms. It reverses a char array "in-place" before printing.

public static void reverseString(char[] s) {
    int len = s.length;

    if (len == 0)
        return;

    for (int i=0; i < (len/2); i++)
    {
        char l = s[i];
        s[i] = s[len-i-1];
        s[len-i-1] = l;
    }

    System.out.println(s);
}
查看更多
闹够了就滚
4楼-- · 2020-03-07 12:30

You can make use of StringBuilder#reverse() method like this:

String reverse = new StringBuilder(new String(letters)).reverse().toString();
查看更多
Ridiculous、
5楼-- · 2020-03-07 12:35

`

//not only prints the reverse order, but creates new char array with chars in desired order
char[] letters = {'e', 'v', 'o', 'l', '4'};
int i = letters.length - 1, j = 0;
char[] let = new char[letters.length];
while(i >= 0){
     let[j] = letters[i];
     i--;
     j++;
}
for (char c : let){
     System.out.print(c);
}

`

output: 4love

查看更多
爷、活的狠高调
6楼-- · 2020-03-07 12:35

Man you code is right except some minor changes in the main method and in the loop and the method has to be static.

The signature printReverse(char[] letters, int size) means that when you call it, you have to pass char array and the size of the array

Try the following

import java.util.Arrays;
import java.util.Collections;

public class search {

    public static void main(String[] args) {          

   char[] letters = {'e', 'v', 'o', 'l', '4'};

   printReverse(letters,5);

}

public static void printReverse(char[] letters, int size){

    for (int i = size-1; i >= 0 ; i--)
    {

     System.out.print(letters[i]);
    }
  }

 }
查看更多
Rolldiameter
7楼-- · 2020-03-07 12:36
public void printReverse(char[] word){

reverseWordMaxIndex = word.length -1;

char[] reverseWord = new char(reverseWordMaxIndex + 1)

for ( int i= 0; i < word.length; i++){

reverseWord[reverseWordMaxIndex-i] = word[i];

}

for( int i= 0; i < reverseWord.length; i++){
  System.out.println(reverseWord[i]);
   }

}
查看更多
登录 后发表回答