Java(在字符串中移动点)
我有一个字符串=“ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种方法。这使用
StringBuilder
以及一个普通的char数组,以避免在数组上进行另一个循环以构建最后一个字符串,但因此会消耗更多的内存。首先,我打印第一个所需的输出,这只是未修改的输入字符串。然后,我创建了一个
StringBuilder
,可以将所有字符从输入中 +一个用于所选分离器的chars +一个字符,以避免不必要的数组调整大小。然后,我初始化StringBuilder
,以便它以第二个所需的Ouput的形式[Char,sep,char,...]
。我在这里使用StringBuilder
,因为它更方便,因为它具有我在这里需要的append()
函数。最后但并非最不重要的一点是,我还初始化了一个char数组,该数组将保存最后一个字符串的值,以避免在数组上迭代两次以生成它。
现在,我从一个(从一个已经初始化到第一个结果)开始到最后一个字符开始
StringBuilder
。在这个循环中,我做三件事。StringBuilder
的当前值StringBuilder
中的下一个字符交换循环后,计算最后的所需输出,我只需要将其打印到控制台即可。
在Bigo通知中的运行时间将为
o(n)
。预期输出:
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 theStringBuilder
so that it is in the form of the second desired ouput[char, sep, char, ...]
. I am usingStringBuilder
here because it is just more convenient as it has theappend()
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.StringBuilder
StringBuilder
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)
.Expected output: