Java(在字符串中移动点)

发布于 2025-01-17 13:17:49 字数 529 浏览 0 评论 0原文

我有一个字符串=“ ABC”; 而且我也有一点“。”。 我如何移动这一点”。在该字符串中(“ ABC”)。 例子 : 输入日期=“ ABC”。 输出日期=“ ABC”,“ A.BC”,“ AB.C”,“ ABC”。

谢谢'。

public class MovePoint {
    public static void main(String[] args) {
        String str = "abcd";
        String str1 = ".";
        String[] ara = new String[str.length()];

        for (int i = 0; i < str.length(); i++) {
            ara[i] = str.substring(i, 1) + str1 + str.substring(1, 2);
            System.out.print(Arrays.toString(ara));

            }
        }
}

I have a string = "abc";
And also I have a point ".".
How I can move this point "." in that string("abc").
Example :
Input date = "abc".
Output date = "abc", "a.bc", "ab.c", "a.b.c".

Thanks'.

public class MovePoint {
    public static void main(String[] args) {
        String str = "abcd";
        String str1 = ".";
        String[] ara = new String[str.length()];

        for (int i = 0; i < str.length(); i++) {
            ara[i] = str.substring(i, 1) + str1 + str.substring(1, 2);
            System.out.print(Arrays.toString(ara));

            }
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2025-01-24 13:17:49

这是一种方法。这使用StringBuilder以及一个普通的char数组,以避免在数组上进行另一个循环以构建最后一个字符串,但因此会消耗更多的内存。

首先,我打印第一个所需的输出,这只是未修改的输入字符串。然后,我创建了一个StringBuilder,可以将所有字符从输入中 +一个用于所选分离器的chars +一个字符,以避免不必要的数组调整大小。然后,我初始化StringBuilder,以便它以第二个所需的Ouput 的形式[Char,sep,char,...]。我在这里使用StringBuilder,因为它更方便,因为它具有我在这里需要的append()函数。

最后但并非最不重要的一点是,我还初始化了一个char数组,该数组将保存最后一个字符串的值,以避免在数组上迭代两次以生成它。

现在,我从一个(从一个已经初始化到第一个结果)开始到最后一个字符开始StringBuilder。在这个循环中,我做三件事。

  1. 打印出StringBuilder的当前值
  2. StringBuilder中的下一个字符交换
  3. 将分隔符

循环后,计算最后的所需输出,我只需要将其打印到控制台即可。

在Bigo通知中的运行时间将为o(n)

public static void main(String[] args) {
    String str = "abcd";
    char sep = '.';
    movePoint(str, sep);
}

public static void movePoint(String str, char sep){
    // print first desired output
    System.out.println(str);
    // String builder that can hold str.length + 1 characters, so no unnecessary resizing happens
    var sb = new StringBuilder(str.length() + 1); 
    // fill with first char
    sb.append(str.charAt(0));
    // add separator
    sb.append(sep);
    // add rest of the string
    sb.append(str.substring(1));
    // Array that holds the last string
    var lastStr = new char[str.length() + str.length() - 1];
    
    for (int i = 1; i < sb.capacity() - 1; i++) {
        System.out.println(sb);
        // build current string
        // swap separator with next character
        var temp = sb.charAt(i);
        sb.setCharAt(i, sb.charAt(i+1));
        sb.setCharAt(i+1, temp);
        // manipulate char array so last string is built correctly
        int doubled = i << 1;
        // set character at correct position
        lastStr[doubled - 2] = sb.charAt(i-1);
        // set separator at correct position
        lastStr[doubled - 1] = sep;
    }
    // add last character of string to this char array
    lastStr[lastStr.length - 1] = sb.charAt(sb.length() - 2);
    // print last desired output
    System.out.println(lastStr);
}

预期输出:

abcd
a.bcd
ab.cd
abc.d
a.b.c.d

Here is one way to do it. This uses StringBuilder as well as a plain char array to avoid having another loop over the array to build the last String, but it therefore consumes more memory.

First I print the first desired output, which is just the unmodified input String. Then I create a StringBuilder which can hold all chars from the input + one more for the chosen separator to avoid unnecessary array resizing. Then I initialize the StringBuilder so that it is in the form of the second desired ouput [char, sep, char, ...]. I am using StringBuilder here because it is just more convenient as it has the append() function that I need here.

Last but not least I also initialize a char array which will hold the values for the last String to avoid having to iterate over the array twice to generate that.

Now I loop over over the StringBuilder starting from one (as its already initialize to the first result with separator) to the last character. In this loop I do three things.

  1. Print out the current value of StringBuilder
  2. Swap the separator with the next character in the StringBuilder
  3. Put the character and separator to the correct position in the char array as required for the last string

After the loop the last desired output is computed and I just have to print it to the console.

Runtime for this in BigO-notation would be O(n).

public static void main(String[] args) {
    String str = "abcd";
    char sep = '.';
    movePoint(str, sep);
}

public static void movePoint(String str, char sep){
    // print first desired output
    System.out.println(str);
    // String builder that can hold str.length + 1 characters, so no unnecessary resizing happens
    var sb = new StringBuilder(str.length() + 1); 
    // fill with first char
    sb.append(str.charAt(0));
    // add separator
    sb.append(sep);
    // add rest of the string
    sb.append(str.substring(1));
    // Array that holds the last string
    var lastStr = new char[str.length() + str.length() - 1];
    
    for (int i = 1; i < sb.capacity() - 1; i++) {
        System.out.println(sb);
        // build current string
        // swap separator with next character
        var temp = sb.charAt(i);
        sb.setCharAt(i, sb.charAt(i+1));
        sb.setCharAt(i+1, temp);
        // manipulate char array so last string is built correctly
        int doubled = i << 1;
        // set character at correct position
        lastStr[doubled - 2] = sb.charAt(i-1);
        // set separator at correct position
        lastStr[doubled - 1] = sep;
    }
    // add last character of string to this char array
    lastStr[lastStr.length - 1] = sb.charAt(sb.length() - 2);
    // print last desired output
    System.out.println(lastStr);
}

Expected output:

abcd
a.bcd
ab.cd
abc.d
a.b.c.d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文