java 如何修复java中的字符串太长异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36513922/
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 fix string too long exception in java
提问by Nuwan Indika
I have a algorithm to analysis string but it take only about 10000 characters more than that it will throw following exception
我有一个分析字符串的算法,但它只需要大约 10000 个字符,就会抛出以下异常
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - constant string too long
this is the code I'm using
这是我正在使用的代码
public static void printRepeatingStrings(String inputString, int sequenceLength) {
if (inputString.isEmpty() || sequenceLength <= 0 || sequenceLength >= inputString.length()) {
System.out.println("Invalid input");
} else {
int i = 0;
int j = i + sequenceLength;
Set<String> tempSet = new HashSet<>();
Set<String> repeatingSequences = new TreeSet<>();
while (j <= inputString.length()) {
if (!tempSet.add(inputString.substring(i, j))) {
repeatingSequences.add(inputString.substring(i, j));
}
i++;
j = i + sequenceLength;
}
for (String str : repeatingSequences) {
System.out.println(str);
}
}
}
If you can help me to fix this that will be very helpful
如果你能帮我解决这个问题,那将非常有帮助
采纳答案by Turo
See Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse
请参阅Java“常量字符串太长”编译错误。仅在使用 Ant 时发生,在使用 Eclipse 时不发生
there ist stated that the length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding.
有说明在 UTF-8 编码中,类文件中字符串常量的长度限制为 2^16 个字节。
If you don't use such a long constant, maybe the otpimizing compiler makes some concatenation with static expressions in the main function.
如果不使用这么长的常量,可能优化编译器会在 main 函数中与静态表达式进行一些连接。
回答by Geeth Lochana
You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.
您应该能够获得长度为 Integer.MAX_VALUE 的字符串(Java 规范始终为 2147483647 (231 - 1),数组的最大大小,String 类用于内部存储)或最大堆大小的一半(因为每个字符是两个字节),以较小者为准。