ดังนั้นคุณต้องการหลีกเลี่ยงการวนซ้ำ?
ที่นี่คุณมีมัน:
public static String repeat(String s, int times) {
    if (times <= 0) return "";
    else return s + repeat(s, times-1);
}
(แน่นอนฉันรู้ว่านี่น่าเกลียดและไม่มีประสิทธิภาพ แต่ไม่มีลูป :-p)
คุณต้องการให้เรียบง่ายและสวยกว่ากัน? ใช้ jython:
s * 3
แก้ไข : ลองเพิ่มประสิทธิภาพหน่อย :-D
public static String repeat(String s, int times) {
   if (times <= 0) return "";
   else if (times % 2 == 0) return repeat(s+s, times/2);
   else return s + repeat(s+s, times/2);
}
แก้ไข 2 : ฉันทำเกณฑ์มาตรฐานที่รวดเร็วและสกปรกสำหรับตัวเลือกหลัก 4 ตัว แต่ฉันไม่มีเวลารันหลาย ๆ ครั้งเพื่อให้ได้ค่าเฉลี่ยและวางแผนเวลาสำหรับอินพุตหลายตัว ... ดังนั้นนี่คือรหัสถ้าใครต้องการ ลองมัน:
public class Repeat {
    public static void main(String[] args)  {
        int n = Integer.parseInt(args[0]);
        String s = args[1];
        int l = s.length();
        long start, end;
        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            if(repeatLog2(s,i).length()!=i*l) throw new RuntimeException();
        }
        end = System.currentTimeMillis();
        System.out.println("RecLog2Concat: " + (end-start) + "ms");
        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            if(repeatR(s,i).length()!=i*l) throw new RuntimeException();
        }               
        end = System.currentTimeMillis();
        System.out.println("RecLinConcat: " + (end-start) + "ms");
        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            if(repeatIc(s,i).length()!=i*l) throw new RuntimeException();
        }
        end = System.currentTimeMillis();
        System.out.println("IterConcat: " + (end-start) + "ms");
        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            if(repeatSb(s,i).length()!=i*l) throw new RuntimeException();
        }
        end = System.currentTimeMillis();
        System.out.println("IterStrB: " + (end-start) + "ms");
    }
    public static String repeatLog2(String s, int times) {
        if (times <= 0) {
            return "";
        }
        else if (times % 2 == 0) {
            return repeatLog2(s+s, times/2);
        }
        else {
           return s + repeatLog2(s+s, times/2);
        }
    }
    public static String repeatR(String s, int times) {
        if (times <= 0) {
            return "";
        }
        else {
            return s + repeatR(s, times-1);
        }
    }
    public static String repeatIc(String s, int times) {
        String tmp = "";
        for (int i = 0; i < times; i++) {
            tmp += s;
        }
        return tmp;
    }
    public static String repeatSb(String s, int n) {
        final StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++) {
            sb.append(s);
        }
        return sb.toString();
    }
}
มันใช้เวลา 2 ข้อโต้แย้งข้อแรกคือจำนวนการวนซ้ำ (แต่ละฟังก์ชั่นทำงานโดยมีเวลาทำซ้ำ arg จาก 1..n) และที่สองคือสตริงที่จะทำซ้ำ
จนถึงตอนนี้การตรวจสอบอย่างรวดเร็วของเวลาที่ทำงานด้วยอินพุตที่แตกต่างกันทำให้การจัดอันดับเป็นแบบนี้ (ดีกว่าแย่กว่า):
- เพิ่มซ้ำ StringBuilder ผนวก (1x)
- การเชื่อมต่อแบบเรียกซ้ำการล็อกแบบเรียกซ้ำ (~ 3x)
- การเชื่อมโยงการเรียกซ้ำเชิงซ้อนแบบเรียกซ้ำ (~ 30x)
- การต่อข้อมูลแบบวนซ้ำเชิงเส้น (~ 45x)
ฉันไม่เคยเดาเลยว่าฟังก์ชั่นวนซ้ำนั้นเร็วกว่าforลูป: -o
ขอให้สนุก (ctional xD)