Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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 如何限制while(iterator.hasNext())迭代?_Java_Netbeans_While Loop_Infinite Loop - Fatal编程技术网

Java 如何限制while(iterator.hasNext())迭代?

Java 如何限制while(iterator.hasNext())迭代?,java,netbeans,while-loop,infinite-loop,Java,Netbeans,While Loop,Infinite Loop,我正在使用Generex库开发Java,以便根据给定的正则表达式打印字符串 有些R.E可以生成无限字符串,我只是想处理它们,但还不能。 我的代码看起来像 Generex generex = new Generex(regex); Iterator iterator = generex.iterator(); System.out.println("Possible strings against the given Regular Expression;\n"); while (

我正在使用Generex库开发Java,以便根据给定的正则表达式打印字符串

有些R.E可以生成无限字符串,我只是想处理它们,但还不能。 我的代码看起来像

Generex generex = new Generex(regex);
Iterator iterator = generex.iterator();
    System.out.println("Possible strings against the given Regular Expression;\n");
    while (iterator.hasNext()) {
        System.out.print(iterator.next() + " ");
    }
如果我将(a)*作为正则表达式输入,则输出应如下所示

a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa ...

如何限制该循环的结果?

假设您希望打印前8项,然后添加
“…”
,如果有更多项要打印。您可以按如下方式进行操作:

int limit = 8;
int current = 0;
while (iterator.hasNext()) {
    if (current != 0) {
        System.out.print(" ");
    }
    System.out.print(iterator.next());
    // If we reach the limit on the number of items that we print,
    // break out of the loop:
    if (++current == limit) {
        break;
    }
}
// When we exit the loop on break, iterator has more items to offer.
// In this case we should print an additional "..." at the end
if (iterator.hasNext()) {
    System.out.print(" ...");
}

假设您希望打印前8项,然后添加
“…”
,如果有更多项要打印。您可以按如下方式进行操作:

int limit = 8;
int current = 0;
while (iterator.hasNext()) {
    if (current != 0) {
        System.out.print(" ");
    }
    System.out.print(iterator.next());
    // If we reach the limit on the number of items that we print,
    // break out of the loop:
    if (++current == limit) {
        break;
    }
}
// When we exit the loop on break, iterator has more items to offer.
// In this case we should print an additional "..." at the end
if (iterator.hasNext()) {
    System.out.print(" ...");
}

在您的情况下,我认为字符串的长度比打印的元素数重要得多,因此我认为以下解决方案可能更好:

Generex generex = new Generex(regex);
Iterator iterator = generex.iterator();
System.out.println("Possible strings against the given Regular Expression;\n");
StringBuilder sb = new StringBuilder();
int limitOfChars = 100; //for example
while (iterator.hasNext()) {
    String next = iterator.next();
    if (sb.length() + next.length() > limitOfChars) break;
    sb.append(next + " ");
}
System.out.println(sb.toString() + " ... ");

在您的情况下,我认为字符串的长度比打印的元素数重要得多,因此我认为以下解决方案可能更好:

Generex generex = new Generex(regex);
Iterator iterator = generex.iterator();
System.out.println("Possible strings against the given Regular Expression;\n");
StringBuilder sb = new StringBuilder();
int limitOfChars = 100; //for example
while (iterator.hasNext()) {
    String next = iterator.next();
    if (sb.length() + next.length() > limitOfChars) break;
    sb.append(next + " ");
}
System.out.println(sb.toString() + " ... ");

标准是什么?你试过维护一个计数器吗?例如,
intMaxIterations=10;while(iterator.hasNext())&&maxIterations-->0){…}
。如果不想创建长度大于N的字符串,可以提出类似的解决方案。有用的。:)正则表达式的条件是只使用{a,b,(,),*}。对每一个R.E,可能的或者如果无限的话,一些字符串会被打印出来,那么标准是什么呢?你试过维护计数器吗?例如,
intMaxIterations=10;while(iterator.hasNext())&&maxIterations-->0){…}
。如果不想创建长度大于N的字符串,可以提出类似的解决方案。有用的。:)正则表达式的条件是只使用{a,b,(,),*}。对于每个R.E,可能的或如果无限的一些字符串将被打印出来,或者如此..在while条件中使用“&&”,而不是使用“break”@boly38,这是另一个选项,是的。我决定设置一个
break
,因为限制逻辑在某种程度上与“主”迭代停止逻辑是分开的,但这是个人偏好的问题。使用“&&”而不是使用“break”@boly38将限制比较放入while条件,这是另一个选项,是的。我决定设置一个
中断
,因为在某种程度上,限制逻辑与“主”迭代停止逻辑是分开的,但这是个人偏好的问题。