简体   繁体   English

使用递归在Java中创建Mobius环

[英]Creating a Mobius Ring in Java using Recursion

So, the assignment requires the method public static String[] mobius(String s1, String s2). 因此,该分配需要方法public static String [] mobius(String s1,String s2)。 And a sample output is to be as follows, provided s1 = "Hello" and s2 = "Java": 假设s1 =“ Hello”和s2 =“ Java”,输出示例如下:

HelloJava HelloJava

elloJavaH elloJavaH

lloJavaHe lloJavaHe

loJavaHel loJavaHel

oJavaHell oJavaHell

JavaHello JavaHello

avaHelloJ avaHelloJ

vaHelloJa vaHelloJa

aHelloJav aHelloJav

This question ( Shifting characters in a string to the left ) got me started on the idea shown below, as the characters of each concatenated string are shifted left, much like an array shift. 这个问题( 将字符串中的字符向左移动 )使我开始了如下所示的想法,因为每个串联字符串的字符都向左移位,就像数组移位一样。 Here's what I have so far: 这是我到目前为止的内容:

public static String[] mobius(String s1, String s2) {
    String combo = new String(s1 + s2);
    String[] mobius = new String[combo.length()];

    mobius[0] = s1 + s2;
    if (method4count < mobius.length) {

        mobius[method4count] = cyclicLeftShift(s1 + s2, method4count++);

        mobius(s1, s2);

    } else {
        return mobius;
    }
    return mobius;

}

public static String cyclicLeftShift(String s, int k) {
    k = k % s.length();
    return s.substring(k) + s.substring(0, k);
}

As of this question, the output is (When printing out the indexes of the String[]): 关于这个问题,输出为(当打印出String []的索引时):

HelloJava HelloJava

elloJavaH elloJavaH

and the remaining elements are null. 其余元素为null。

For reference, this is the "Test" Case that's been given to me: 作为参考,这是给我的“测试”案例:

String s1 = "Hello", s2 = "Java";
    String[] strs = mobius(s1, s2);
    for (String s : strs)
        System.out.println(s);

UPDATE: (Forgot to mention this initially, sorry about that) I am not allowed to use loops for this problem whatsoever. 更新:(最初忘了提起这个,对此感到抱歉)无论如何我都不允许使用循环。 Thanks to those who provided the answers before! 感谢以前提供答案的人!

Is there either an easier method (or just a method) to fill the remainder of the array (Not pertaining to the test case, but to the mobius method and to the cycleLeftShift method)? 是否存在一种更简单的方法(或只是一种方法)来填充数组的其余部分(与测试用例无关,但与mobius方法和cycleLeftShift方法无关)? Or am I missing something that's probably obvious? 还是我错过了可能很明显的东西?

Thanks to anyone and everyone in advance! 提前感谢任何人和每个人! Also, sorry for the long "question" as it were 另外,很抱歉长期以来的“疑问”

public class NewClass1 {

    private static int method4count = 1;
    static String[] mobius ;

    public static void main(String args[]){
        String s1 = "Hello", s2 = "Java";
        mobius = new String[(s1+s2).length()];

        String[] strs = mobius(s1, s2);        
        for (String s : strs)
            System.out.println(s);
    }
    public static String[] mobius(String s1, String s2) {      
        mobius[0] = s1 + s2;
        if (method4count < mobius.length) {
            mobius[method4count] = cyclicLeftShift(s1 + s2, method4count++);
            mobius(s1, s2);
        } else {
            return mobius;
        }
        return mobius;
    }

    public static String cyclicLeftShift(String s, int k) {
        k = k % s.length();
        return s.substring(k) + s.substring(0, k);
    }
}
private static void cycle(String s, int cases) {
    if (cases > 0) {
        String temp = cases == s.length() ? s : s.substring(1, s.length()) + s.charAt(0);
        System.out.println(temp);
        cycle(temp, cases - 1);
    }
}

This will perform what you request. 这将执行您的请求。 It uses recursion and an integer denoting how many more cases it needs to perform. 它使用递归和一个整数来表示需要执行多少个案例。 All it does is substring from index 1 to the end of the string and then append the current first letter to the back of the string. 它所做的只是从索引1到字符串结尾的子字符串,然后将当前的第一个字母附加到字符串的后面。

Firstly, thanks to everyone who helped me solve the question! 首先,感谢所有帮助我解决问题的人!

Secondly; 其次; here is the answer I arrived at (from this help and the help of friends and more experienced coders: 这是我得出的答案(在此帮助以及朋友和更有经验的编码人员的帮助下:

static int method4count = 1;
static String combine = "";
static String[] array;

public static String[] mobius(String s1, String s2) {
    combine = s1 + s2;

    if (method4count == 1) {
        array = new String[combine.length()];
        array[0] = s1 + s2;
    }
    if (method4count < combine.length()) {
        array[method4count] = cyclicLeftShift(s1 + s2, method4count++);
        mobius(s1, s2);
    }

    return array;
}

public static String cyclicLeftShift(String s, int k) {
    k = k % s.length();
    return s.substring(k) + s.substring(0, k);
}

Test case is not included in the above work; 测试用例不包括在上述工作中; what a TA and I arrived at was that with the static int method4count starting at 1, there is a conditional statement that, if returned true; 我得到的TA就是,静态int method4count从1开始,有一个条件语句,如果返回true; initializes the String array to the length of the concatenated string "combine." 将String数组初始化为连接的字符串“ combine”的长度。 Then the array is filled through a recursive call in the second if statement that checks to see if the counter is less than the length of "combine," and if true, assigns the next variant of the string in the mobius ring (through the method cyclicShiftLeft) before incrementing method4count up by one and recursively calling the mobius method. 然后,通过第二个if语句中的递归调用来填充数组,该语句检查计数器是否小于“ combine”的长度,如果为true,则在mobius环中分配字符串的下一个变体(通过方法)循环ShiftLeft),然后将method4递增1,然后递归调用mobius方法。 Once this array is filled and the second if statement proves to be false, then the array is returned. 一旦该数组被填充并且第二个if语句证明为false,则返回该数组。

Once again, thanks to everyone who helped with this problem! 再次感谢所有为这个问题提供帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM