有没有办法在简单的FOR循环中初始化不同的类型

时间:2015-08-12 13:57:44

标签: java for-loop

我正在尝试初始化简单FOR循环的初始化表达式中的不同类型,但是有一个错误 - Type mismatch: cannot convert from BigInteger to int; Syntax error on token "bigInt2", delete this token

我尝试BigInteger,但即使我为原语做,那么我也会得到错误。

以下是我的尝试。

for (int j = 1, BigInteger bigInt2 = new BigInteger("1"); 
     j < 1000000; j++, bigInt2 = bigInt2.add(bigIntBase)) {

问题:

  • 我做错了什么还是不允许?
  • 如果不允许那么为什么?我知道允许在增量表达式中使用不同的类型。


解决方法我知道:
我知道我可以在FOR循环之前初始化它,但是它打破了初始化表达式的全部目的,因为如果发生这种情况,那么整个初始化表达式可能已经从FOR循环中删除了,开发人员可以在FOR循环之前完成它。

5 个答案:

答案 0 :(得分:1)

如JSL §14.14.1中所述:

  

basic语句执行某些初始化代码,然后重复执行Expression,Statement和一些更新代码,直到Expression的值为false。

<强> 14.14.1.1。声明初始化

  

首先执行ForInit代码执行for语句:

     

如果ForInit代码是语句表达式列表(§14.8),则表达式按从左到右的顺序进行计算;它们的价值(如果有的话)被丢弃。

内部(§14.8)你可以找到:

  

如果需要技巧,可以使用赋值语句(§15.26)或局部变量声明语句(§14.4)。

阅读(§14.4

  

局部变量声明中的每个声明符声明一个局部变量,其名称是声明符中出现的标识符。

所以......你可以在for循环的语句声明中使用多个计数器 语句声明只能有一个类型

所以要将这两个变量创建为BigInteger

for (
    // definition
    BigInteger j = BigInteger.ONE, 
         bigInt2 = BigInteger.ONE;
    // comparation  
    j.compareTo(BigInteger.valueOf(1000000)) > 0; 
    // increment
    j.add(BigInteger.ONE), bigInt2 = bigInt2.add(bigIntBase)
) {

或者,只需手动处理int计数器并循环遍历BigInteger

int j = 1;
for (BigInteger bigInt2 = BigInteger.ONE; ;bigInt2 = bigInt2.add(bigIntBase)) {
    // do your stuff inside for loop 

    // manually handle int counter
    if (j>1000000) break;
    else j++;
}

答案 1 :(得分:1)

您使用的synthax用于初始化相同类型的变量。喜欢:

int i=0, j=3; //works fine

您不能像这样实例化不同类型的变量:

int i=0, double j=3; //does not work

但这就是你要在for循环中尝试做的事情。这不能以任何方式工作,这不是由for循环引起的。 Java不能像这样工作(即使其他语言也这样)。

您必须使用解决方法来实现您想要达到的目标。

祝你好运,

的Mathias

答案 2 :(得分:1)

我假设这个for循环将成为类的一部分...那么为什么不简单地将整数和BigInteger作为类属性添加并将类型定义保留在for循环之外呢?

这样的事情:

set webServerPath=

echo -- Name --

set /p name=": "

findstr /r /i "%name%" %webServerPath%\CopiedMovedFiles\ *.txt>%webServerPath%\Found.txt

findstr /r /i "%name%" %webServerPath%\TarPackages\ *.txt>>%webServerPath%\Found.txt

findstr /r /i "%name%" %webServerPath%\TaskErrors\ *.txt>>%webServerPath%\Found.txt

Start notepad.exe %webServerPath%\Found.txt

pause

答案 3 :(得分:1)

你为什么不这样做:

    for (final Wrapper w = new Wrapper(0, new BigInteger("0"));
             w.stop(1000);
             w.plus(1, bigIntBase))
    {
        //do your weird stuff
    }

使用包装类:

public static class Wrapper
{
    private int              i;

    private final BigInteger bigI;

    public Wrapper(final int pI,
                   final BigInteger pBg)
    {
        super();
        i = pI;
        bigI = pBg;
    }

    public void plus(final int dI,
                     final BigInteger bi)
    {
        i += dI;
        bigI.add(bi);
    }

    public boolean stop(final int max)
    {
        return i<max;
    }

     //add getter for i and bigI...
}

它应该没有?

的Mathias

答案 4 :(得分:1)

不,这是不允许的。

来自the JLS (14.14)的相关引文:

for语句定义为:

  

for(ForInit opt ; Expression opt ; ForUpdate opt )...

初始化部分定义为:

  

ForInit:
   StatementExpressionList
   LocalVariableDeclaration

StatementExpressionList(14.14 + 14.8)我们想要什么,因为那里没有变量声明:

  

StatementExpressionList:
   StatementExpression
   StatementExpressionList,StatementExpression

     

StatementExpression:
   分配
   PreIncrementExpression
   PreDecrementExpression
   PostIncrementExpression
   PostDecrementExpression
   的MethodInvocation
   ClassInstanceCreationExpression

所以LocalVariableDeclaration就是你想要的。这只允许单一类型:(14.4)

  

LocalVariableDeclaration:
   VariableModifiers opt 类型VariableDeclarators

您可以按照相同的流程确定ForInitForUpdate 允许的内容。