java for 循环,通过字母表迭代?爪哇
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33163253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for loop, iteration through alphabet? java
提问by pewpew
I can iterate through the alphabet, but I'm trying to keep the last iteration and add on the next letter. this is my code.
我可以遍历字母表,但我试图保留最后一次迭代并添加下一个字母。这是我的代码。
for(char alphabet = 'a'; alphabet <='z'; alphabet ++ )
{
System.out.println(alphabet);
}
I want it to print out something that looks like this.
我希望它打印出看起来像这样的东西。
a
一个
ab
AB
abc
美国广播公司
abcd
A B C D
abcde..... and so forth. How is this possible?
abcde..... 等等。这怎么可能?
回答by Stefan
You need to add the char alphabet
to a string.
您需要将char alphabet
加到字符串中。
String output = "";
for(char alphabet = 'a'; alphabet <='z'; alphabet++ )
{
output += alphabet;
System.out.println(output);
}
This should work for you ;)
这应该适合你;)
回答by Ambrish
I will go with StringBufferor StringBuilder. Something like:
我将使用StringBuffer或StringBuilder。就像是:
StringBuffer
StringBuffer
StringBuffer sb = new StringBuffer();
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
sb.append(alphabet);
System.out.println(sb.toString());
}
StringBuilder
StringBuilder
StringBuilder sb = new StringBuilder();
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
sb.append(alphabet);
System.out.println(sb.toString());
}
String vs StringBuffer vs StringBuilder:
字符串与 StringBuffer 与 StringBuilder:
String: It is immutable, so when you do any modification in the string, it will create new instance and will eatup memory too fast.
String: 它是不可变的,所以当你对字符串做任何修改时,它会创建新的实例并且会过快地消耗内存。
StringBuffer: You can use it to create dynamic String and at the sametime only 1 object will be there so very less memory will be used. It is synchronized (which makes it slower).
StringBuffer:您可以使用它来创建动态字符串,同时只有 1 个对象将在那里,因此将使用非常少的内存。它是同步的(这使它变慢)。
StringBuilder: It is similar to StringBuffer. The olny difference is: it not synchronized and hence faster.
StringBuilder:它类似于 StringBuffer。最大的区别是:它不同步,因此速度更快。
So, better choice would be StringBuilder. Read more.
所以,更好的选择是StringBuilder。阅读更多。
Using Java 8
使用 Java 8
StringBuilder sb = new StringBuilder();
IntStream.range('a', 'z').forEach(i -> {
sb.append((char) i);
System.out.println(sb.toString());
});
回答by Andreas
I'd suggest using a StringBuilder
:
我建议使用StringBuilder
:
// Using StringBuilder
StringBuilder buf = new StringBuilder();
for (char c = 'a'; c <= 'z'; c++)
System.out.println(buf.append(c).toString());
You could also do it slightly faster by using a char[]
, however StringBuilder
is more obvious and easier to use:
你也可以通过使用 a 稍微快一点char[]
,但是StringBuilder
更明显和更容易使用:
// Using char[]
char[] arr = new char[26];
for (int i = 0; i < 26; i++) {
arr[i] = (char)('a' + i);
System.out.println(new String(arr, 0, i + 1));
}
Alternatives that you shouldn't use:
你不应该使用的替代品:
StringBuffer
: Same asStringBuilder
, but synchronized, so slower.s = s.concat(new String(c))
: Allocates 2 Strings per iteration, instead of only 1.s += c
: Internally+=
is compiled tos = new StringBuilder().append(s).append(c).toString()
, so horrendously slow with exponentialresponse times.
StringBuffer
: 和 一样StringBuilder
,但是是同步的,所以比较慢。s = s.concat(new String(c))
:每次迭代分配 2 个字符串,而不是仅分配 1 个。s += c
:在内部+=
编译为s = new StringBuilder().append(s).append(c).toString()
,因此响应时间呈指数级增长,速度非常慢。
回答by Arpit Aggarwal
Offtopic, Using Java 8
IntStreamit is as simple as:
Offtopic,使用Java 8
IntStream就像这样简单:
StringBuilder builder = new StringBuilder("");
"abcdefghijklmnopqrstuvwxyz".chars().mapToObj(i -> builder.append((char) i))
.forEach(System.out::println);
Usage:
用法:
public class AlphabetsLooping {
static final String input = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("");
input.chars().mapToObj(i -> builder.append((char) i))
.forEach(System.out::println);
}
}
回答by Rehman
String s = "" ;
for(char alphabet = 'a'; alphabet <='z'; alphabet ++ )
{
s = s.concat(String.valueOf(alphabet));
System.out.println(s);
}