如何在java中创建一个数字序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36568590/
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
How to create a sequence of numbers in java
提问by
I want to create a sequence of numbers in java like this :
我想在 java 中创建一个数字序列,如下所示:
1234957203969304597600234960702349100903450234847456282934857697900389262454869346
I want to create 1000 numbers in the sequence ..
我想在序列中创建 1000 个数字..
How can I do it?
我该怎么做?
I tried to do like this :
我试着这样做:
String seq=null;
for (int i=0; i<1000; i++) {
seq=String.format("%d",i);
}
System.out.println(seq);
it does not work , it prints out : 999
它不起作用,它打印出来: 999
thanks
谢谢
采纳答案by JimmyB
StringBuilder sb = new StringBuilder();
for (int i=0; i<1000; i++) {
sb.append(i);
}
System.out.println(sb.toString());
As a general note, while str = str + someString;
will work, inside a loop it can quickly get out of hand. Try it with 10000 iterations and you'll see (large amounts of RAM & CPU consumed).
一般而言,whilestr = str + someString;
会起作用,但在循环中它很快就会失控。尝试 10000 次迭代,您会看到(消耗了大量 RAM 和 CPU)。
StringBuilder
is better, if one really needs to build a string in this way, and it's always better performace-wise when one is appending to a character sequence more than a couple of times.
StringBuilder
更好,如果真的需要以这种方式构建一个字符串,并且当一个人多次附加到一个字符序列时,它总是更好的性能。
回答by Norsk
You need to append it to the String. I suggest using a StringBuilder.
您需要将其附加到字符串。我建议使用 StringBuilder。
What you are doing is overwriting the String every time
你在做什么是每次都覆盖字符串
StringBuilder sb = new StringBuilder();
for(int i=0; i<1000; i++) {
sb.append(Integer.toString(i);
}
System.out.println(sb.toString());
回答by Ankit Shubham
If you want to create a stream of length 1000 with each digit being random, then try Math.random function.
如果你想创建一个长度为 1000 的流,每个数字都是随机的,那么试试 Math.random 函数。
回答by Kevin Cruijssen
To make your code work, change seq=String.format("%d",i);
into seq+=String.format("%d",i);
.
要使您的代码正常工作,请更改seq=String.format("%d",i);
为seq+=String.format("%d",i);
.
A better way however, is to use a StringBuilder
like this:
然而,更好的方法是使用StringBuilder
这样的:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++){
sb.append(i);
}
String seq = sb.toString();
System.out.println(seq);
EDIT: Actually, this doesn't generate a String with length 1000
, since it adds like this: 012345678910...
(> 10 is two or three numbers instead of one).
编辑:实际上,这不会生成长度为 String 的字符串1000
,因为它是这样添加的:012345678910...
(> 10 是两个或三个数字而不是一个)。
So, instead try something like this using the Random
class for a random number of 0-9
:
因此,请尝试使用Random
随机数的类来尝试类似的操作0-9
:
Random randomGenerator = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++){
sb.append(randomGenerator.nextInt(9));
}
String seq = sb.toString();
System.out.println(seq);
回答by ΦXoc? ? Пepeúpa ツ
Your code is printing 999 because you are always overwriting the string with the last value of the loop, you need to append the data, not overwrite it over and over again...
您的代码正在打印 999,因为您总是用循环的最后一个值覆盖字符串,您需要附加数据,而不是一遍又一遍地覆盖它......
Example:
例子:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i);
}
System.out.println(sb.toString());