zoukankan      html  css  js  c++  java
  • Java实现的三种字符串反转

    Java实现的三种字符串反转

    第一种:

    public class Main {
    
        public static void main(String[] args) {
            String s1 = "asdfghjkl";
            System.out.println(new StringBuilder(s1).reverse().toString());
        }
    }

    第二种:

    public class Main {
    
        public static void main(String[] args) {
            String s1 = "asdfghjkl";
            String[] s = s1.split("");
            List<String> list = list = Arrays.asList(s);
            Collections.reverse(list);
            System.out.println(list);
        }
    }

    第三种:

    public class Main {
    
        public static void main(String[] args) {
            String s1 = "asdfghjkl";        System.out.println(new Main().swapWords(s1));
    
        }
    
        public void swap(char[] arr, int begin, int end) {
            while (begin < end) {
                char temp = arr[begin];
                arr[begin] = arr[end];
                arr[end] = temp;
                begin++;
                end--;
            }
        }
    
        public String swapWords(String str) {
            char[] arr = str.toCharArray();
            swap(arr, 0, arr.length - 1);
            int begin = 0;
            for (int i = 1; i < arr.length; i++) {
                if (arr[i] == ' ') {
                    swap(arr, begin, i - 1);
                    begin = i + 1;
                }
            }
            return new String(arr);
        }
    }
  • 相关阅读:
    set-find
    set-equal_range
    set-equal_range
    set-erase
    php 抽象类 静态 单体设计模式
    Servlet 工作原理解析
    职场上一个人情商高的十种表现
    快速学习一门新技术入门
    php中14中排序方式的实现
    php中对Mysql数据库的访问操作
  • 原文地址:https://www.cnblogs.com/wml-it/p/13070733.html
Copyright © 2011-2022 走看看