18 March 2017

Đảo ngược chuỗi sử dụng interface CharSequence có sẵn của hệ thống trong JAVA

Demo.Java
Java Advanced 2017
public class Demo implements CharSequence {

    private final CharSequence original;

    public Demo(CharSequence original) {
        this.original = original;
    }

    @Override
    public char charAt(int index) {
        // TODO Auto-generated method stub
        return original.charAt(original.length() - index - 1);
    }

    @Override
    public int length() {
        // TODO Auto-generated method stub
        return original.length();
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        // TODO Auto-generated method stub
        int originalEnd = original.length() - start;
        int originalStart = original.length() - end;
        return new Java3(original.subSequence(originalStart, originalEnd));
    }

    public String toString() {
        return new StringBuilder(this).toString();
    }

    public static void main(String[] args) {

        Demo = new Demo("Hello Wold 2017");

        // for (int i = 0; i < s.length(); i++) {
        // System.out.print(s.charAt(i));
        // }
        System.out.println(s);
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang