Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_For Loop - Fatal编程技术网

循环字的Java

循环字的Java,java,loops,for-loop,Java,Loops,For Loop,我如何在这里使用==运算符 我还需要帮助,我这里有一个列表数组,我想在数组中添加任何空白的位置 长度只是一个表示单词长度的变量 for (int i = 0; i<length; i++) { if (word.charAt(i)) == (' '){ whitespaces_array.add(i); } for(int i=0;i 在''周围不需要括号,只需将第二个右括号从word.charAt(i)后面移到所需的检查值后面

我如何在这里使用==运算符

我还需要帮助,我这里有一个列表数组,我想在数组中添加任何空白的位置

长度只是一个表示单词长度的变量

for (int i = 0; i<length; i++) {
        if (word.charAt(i)) == (' '){
            whitespaces_array.add(i);
         }
for(int i=0;i
在
''
周围不需要括号,只需将第二个右括号从
word.charAt(i)
后面移到所需的检查值后面


完成后,请在花括号前留出一个空格,就像在for()循环的顶行中那样。Java不需要它,但对大多数人来说它看起来要整洁得多:)

如一条评论所示,实际的问题是您在问题中没有提供足够的信息

在Java中,
Integer
s是对象,而
int
s也是整数,但它们是原始数据类型。
int
或多或少地直接映射到通过算术运算操作的一组简单位。这里我不会深入讨论这些差异,但关键是
Integer!=int

正确的代码看起来更像以下代码:

ArrayList<Integer> whitespaces_array = new ArrayList<Integer>();

for ( int i = 0; i < length; ++i ) {
    if ( word.charAt( i ) == ' ' ) {
        whitespaces_array.add( i );
    }
}
ArrayList whitespaces_array=new ArrayList();
对于(int i=0;i<长度;++i){
如果(字字符(i)=''){
空白_数组。添加(i);
}
}

只想添加一些与此代码相关的帮助我的东西

(ArrayListlist1)


此注释中的第二行是在方法中引用列表数组时输入的参数当它们使用整数时,您不会输入int list1,因为上面提到的int和Integar是不同的

Java要求在整个表达式周围加括号,例如
if(word.charAt(i)=''){…
我在第三行“无法在基本类型int上调用add(int)”@MoR什么是
whitespaces\u array
定义为?可能重复@Jaden ArrayList whitespaces\u array=new ArrayList();我这样做了,最后一个括号上出现语法错误。它说语法错误,insert“}”完成MethodBody@MoR好的,这超出了这个问题和答案的范围。虽然这些是相对基本的问题,但为了学习Java,你应该看一个现代的、最新的教程/课程。好的,没问题,我是Java新手,但我设法解决了这个问题。谢谢
ArrayList<Integer> whitespaces_array = new ArrayList<Integer>();

for ( int i = 0; i < length; ++i ) {
    if ( word.charAt( i ) == ' ' ) {
        whitespaces_array.add( i );
    }
}