简体   繁体   English

找不到符号方法Iterator()

[英]can not find symbol method Iterator()

Hello everyone i have started learning collection in java and trying to write a simple program but i am getting 2 errors please have a look and help me out. 大家好,我已经开始学习Java的集合并尝试编写一个简单的程序,但是我遇到2个错误,请看一下并帮助我。

import java.util.Iterator;
import java.util.*;
    class ArrayListDemo1{
    public static void main(String... s){
    ArrayList<Integer> al = new ArrayList<Integer>();
 // ArrayList al = new ArrayList();
    int x[] = {1, -1, 2, -2, 3, -3, 4, -4};
    for(int i=0; i<x.length;i++){
        al.add(x[i]);
    }
    System.out.println(al);
//  Iterator<Integer> i = al.Iterator();
    Iterator i = al.Iterator();
    while(i.hasNext()){
        Integer z = (Integer)i.next();
//      Integer z = i.next();
        if(z.intValue < 0)  
        i.remove(); 
    }   
    System.out.println(al);
}
}
 1. Iterator i = al.iterator();//here method is case sensitive
 2. intValue() not intValue, is a method.

Java is case sensitive and methods usually start in a lower case letter. Java区分大小写,方法通常以小写字母开头。 It should be al.iterator(); 应该是al.iterator();

Beside that, intValue is a method, so z.intValue should be z.intValue() . 除此之外, intValue是一个方法,因此z.intValue应该是z.intValue()

1st Error 第一个错误

Methods usually follows camelcase and are case-sensitive. 方法通常遵循驼峰式且区分大小写。

It should be al.iterator() 应该是al.iterator()

Please have a look here 在这里看看

2nd Error 第二次错误

intValue() is a function, please refer this . intValue()是一个函数,请参考this

change if(z.intValue < 0) to if(z.intValue() < 0) if(z.intValue < 0)更改为if(z.intValue() < 0)

After Run, output 运行后,输出

[1, -1, 2, -2, 3, -3, 4, -4]
[1, 2, 3, 4]

Tested here 在这里测试

并且您编写了intValue。它为false 它应该为intValue()。

You have 2 compile errors: 您有2个编译错误:

Iterator i = al.Iterator();

in java method names start with a lowercase character, also Iterator is a raw type, you should give it a generic type: 在Java方法名称中,以小写字符开头,并且Iterator也是原始类型,您应该给它一个泛型类型:

Iterator<Integer> i = al.iterator();

your second problem is here: 您的第二个问题在这里:

z.intValue

Integer doesn't have a public field intValue, but a method intValue() that returns the desired value: Integer没有公共字段intValue,但是有一个返回所需值的方法intValue():

z.intValue()

As a hint: I don't know why you use the Iterator here, but you could also use a for-each loop for the task of removing a list item: 提示:我不知道为什么在这里使用Iterator,但是您也可以使用for-each循环来执行删除列表项的任务:

for ( Integer integer : al )
{
    if ( integer.intValue() < 0 )
        al.remove( integer );
}

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

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