How can I get a char array in reverse order?

后端 未结 10 2565
情深已故
情深已故 2020-12-18 16:23

my assignment question is like that

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

void printReverse(char         


        
10条回答
  •  死守一世寂寞
    2020-12-18 16:50

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

提交回复
热议问题