简体   繁体   English

我该怎么做长值循环

[英]How can I do for loop with long value

I need to do codes with for loop and long value like this: 我需要使用for循环和long值执行代码,如下所示:

for(long i=1;i<lines.length; i++){

    char tmp = lines[i].charAt(lines[i].length()-1);

    int index = lines[i].lastIndexOf(" ");

    lines[i] = lines[i].substring(0, index);

    if(tmp != lastChar)
        lines[i] = "S" + lines[i];
    else
        lines[i]=" "+lines[i];

    lastChar = tmp;        
}

The problem is: incompatible types:possible lossy conversion from long to int I don't know how to fix it 问题是: incompatible types:possible lossy conversion from long to int我不知道该如何解决

you are using long in for loop that is correct but when you are using index in lines[i] then problem start. 您正在使用正确的long for循环,但是当您在lines[i]中使用索引时,问题就开始了。 java says index is always int but in your case it is long. Java表示index总是int,但在您的情况下它很长。

and long can not cast directly into int.either change long data type to int data type in for loop or cast i variable when you are using as index like lines[(int)i] (not prefer). 并且long不能直接转换为int。在使用像lines[(int)i]这样的索引时,最好将long数据类型更改为in循环中的int数据类型,或者转换i变量(不推荐使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM