zoukankan      html  css  js  c++  java
  • JAVA面试题之实现字符串的倒序输出

     1 package shb.java.demo;
     2 
     3 public class MyTest {
     4     public static void main(String[] args) {
     5         String string = "For God so loved the world kdkdk";
     6         reverse_2(string);
     7     }
     8     //方法一
     9     public static void reverse(String str){
    10         
    11         String[] strings = str.split(" ");
    12         for(int i= 0 ; i < strings.length;i++){
    13             String top =strings[0];
    14 
    15                      for(int j=1;j<strings.length-i;j++){
    16 
    17                       strings[j-1]= strings[j];
    18 
    19             }
    20                      strings[strings.length-i-1]=top;
    21 
    22             }
    23         for(String string:strings){
    24             System.out.println(string);
    25         }
    26     }
    27     //方法二
    28     public static void reverse_2(String str_2){
    29         String []strings = str_2.split(" ");
    30         String temp = "";
    31         if(strings.length%2==0){
    32         for(int i=0;i<strings.length/2;i++){
    33             temp = strings[i];
    34             strings[i] = strings[strings.length-1-i];
    35             strings[strings.length-1-i] = temp;
    36         }    
    37         for(String string2:strings){
    38             System.out.println(string2);
    39         }
    40         }else {
    41             for(int i=0;i<(strings.length-1)/2;i++){
    42                 temp = strings[i];
    43                 strings[i] = strings[strings.length-1-i];
    44                 strings[strings.length-1-i] = temp;
    45             }    
    46             for(String string2:strings){
    47                 System.out.println(string2);
    48             }
    49         }
    50     }
    51 }
  • 相关阅读:
    2014-12-28 iframe,style,body style
    2014-12-25 网页基础,html。
    2014-12-19 特殊集合:stack、queue、Hashtable
    2014-12-18 集合
    2014-12-16 多维数组
    2014-12-15 数组
    2014-12-14 跳转语句,while循环,各种类的用法
    combobox DataGridView
    面向对象(重载、封装、继承、抽象)
    winform
  • 原文地址:https://www.cnblogs.com/assassin666/p/4997567.html
Copyright © 2011-2022 走看看