Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建一个很长的字符串,并在运行时_Java_String_Time Complexity - Fatal编程技术网

Java 创建一个很长的字符串,并在运行时

Java 创建一个很长的字符串,并在运行时,java,string,time-complexity,Java,String,Time Complexity,有人能帮助减少运行此代码的时间吗 这不是一个实际的代码,它只是我项目的一部分: import java.util.*; class testing{ public static void main(String[] args) { String s=""; for(long i=0L;i<10000000000L;i++) s+=String.valueOf(Character.toChars((int)(Math.ran

有人能帮助减少运行此代码的时间吗

这不是一个实际的代码,它只是我项目的一部分:

import java.util.*;

class testing{
    public static void main(String[] args) {
        String s="";

        for(long i=0L;i<10000000000L;i++)
            s+=String.valueOf(Character.toChars((int)(Math.random()*26+97)));

        System.out.println(s.length()+"\n"+s);
    }
}
import java.util.*;
类测试{
公共静态void main(字符串[]args){
字符串s=“”;
对于(长i=0L;i1),通过设置所需容量实例化
StringBuilder

 new StringBuilder(wishedSize)
2) 您创建了一个随机的
double
,但将其强制转换为
int

(int)(Math.random()*26+97)
因此,您可以在循环之前使用自己的
Random
对象并调用
nextInt()

3) 正如Andy所强调的(我没有注意到),你会在结尾看到一系列小写字母。
您可以使用
Random.nextInt(int)
获取介于97和122之间的数字(小写字母的unicode范围),例如:

final int maxRange = 122 - 97;
Random random = new Random();
for (long i = 0L; i < 100L; i++) {
    int codePoint = random.nextInt(maxRange) + 97;
    // ...
}
final int maxRange=122-97;
随机=新随机();
用于(长i=0L;i<100L;i++){
int codePoint=random.nextInt(最大范围)+97;
// ...
}
此外,通过这种方式,您只对返回的随机值执行1次算术计算,而不是以前的2次。

1)通过设置所需的容量实例化
StringBuilder

 new StringBuilder(wishedSize)
2) 您创建了一个随机的
double
,但将其强制转换为
int

(int)(Math.random()*26+97)
因此,您可以在循环之前使用自己的
Random
对象并调用
nextInt()

3) 正如Andy所强调的(我没有注意到),你会在结尾看到一系列小写字母。
您可以使用
Random.nextInt(int)
获取介于97和122之间的数字(小写字母的unicode范围),例如:

final int maxRange = 122 - 97;
Random random = new Random();
for (long i = 0L; i < 100L; i++) {
    int codePoint = random.nextInt(maxRange) + 97;
    // ...
}
final int maxRange=122-97;
随机=新随机();
用于(长i=0L;i<100L;i++){
int codePoint=random.nextInt(最大范围)+97;
// ...
}

此外,通过这种方式,您只对返回的随机值执行1次算术计算,而不是以前的2次。

您生成的每个字符串都是不可变的,因此添加到字符串中会导致大量的搅动。使用缓冲区,如下所示

class testing{
public static void main(String[] args) {
    StringBuffer sb = new StringBuffer(EST_SIZE);

    for(long i=0L;i<COUNT;i++)
        sb.append( Integer.toString(Math.random()*26+97 ) );

System.out.println(sb.length()+"\n"+sb.toString());
类测试{
公共静态void main(字符串[]args){
StringBuffer sb=新的StringBuffer(EST_大小);

对于(long i=0L;i您生成的每个字符串都是不可变的,因此添加到字符串会导致大量的搅动

class testing{
public static void main(String[] args) {
    StringBuffer sb = new StringBuffer(EST_SIZE);

    for(long i=0L;i<COUNT;i++)
        sb.append( Integer.toString(Math.random()*26+97 ) );

System.out.println(sb.length()+"\n"+sb.toString());
类测试{
公共静态void main(字符串[]args){
StringBuffer sb=新的StringBuffer(EST_大小);

对于(long i=0L;i您创建了许多字符串并将它们连接起来,这相当慢

如果在生成字符串之前已知预期大小,则使用
StringBuilder
可以更快:

StringBuilder sb = new StringBuilder(expectedStringLength);

for (int i = 0; i < expectedStringLength; i++)
    sb.append(Math.random() * 26 + 97);

System.out.println(sb.length() + "\n" + sb.toString());
StringBuilder sb=新StringBuilder(预期StringLength);
对于(int i=0;i

但是请注意,字符串的长度不能大于
integer.MAX_VALUE
,因为它是由数组支持的,java只支持
int
索引。然后,拥有如此大的字符串将使用大量内存,因此您可能需要改变您的概念。

您创建了许多字符串并将它们串联起来,这将速度相当慢

如果在生成字符串之前已知预期大小,则使用
StringBuilder
可以更快:

StringBuilder sb = new StringBuilder(expectedStringLength);

for (int i = 0; i < expectedStringLength; i++)
    sb.append(Math.random() * 26 + 97);

System.out.println(sb.length() + "\n" + sb.toString());
StringBuilder sb=新StringBuilder(预期StringLength);
对于(int i=0;i

但是请注意,字符串的长度不能大于
integer.MAX_VALUE
,因为它是由数组支持的,java只支持
int
索引。然后,拥有如此大的字符串将使用大量内存,因此您可能需要改变您的概念。

为了防止崩溃,完全绕过字符串并使用dum将它直接映射到文件系统

try (OutputStream out = new OutputStreamWriter(new FileOutputStream(file)), "utf-8") {
    for (int i = 0; i < 100000000L; i++) {
        out.write(Character.toChars((int)(Math.random()*26+97)));
    }
    out.flush();
}
try(OutputStream out=newoutputstreamwriter(newfileoutputstream(file)),“utf-8”){
对于(int i=0;i<100000000 l;i++){
out.write(Character.toChars((int)(Math.random()*26+97));
}
out.flush();
}

为了防止崩溃,请完全绕过字符串并将其直接转储到文件系统

try (OutputStream out = new OutputStreamWriter(new FileOutputStream(file)), "utf-8") {
    for (int i = 0; i < 100000000L; i++) {
        out.write(Character.toChars((int)(Math.random()*26+97)));
    }
    out.flush();
}
try(OutputStream out=newoutputstreamwriter(newfileoutputstream(file)),“utf-8”){
对于(int i=0;i<100000000 l;i++){
out.write(Character.toChars((int)(Math.random()*26+97));
}
out.flush();
}

使用预分配的StringBuilder。你能更精确地显示给我吗?这样这个代码会崩溃。数组的最大大小为2**31。用一堆
系统输出打印调用替换字符串生成怎么样?这样就没有办法存储这个大字符串了??使用预分配的StringBuilder。你能更精确地显示这个代码吗将崩溃。数组的最大大小为2**31。用一堆
System.out.print
调用替换字符串构建如何?因此无法存储这个大字符串??使用
StringBuilder
而不是
StringBuffer
。使用
StringBuilder
而不是
StringBuffer
。3)OP正在创建一个随机的小写字母字符串。@Andy Turner很高兴看到这一点。3)OP正在创建一个随机的小写字母字符串。@Andy Turner很高兴看到这一点。