Error: incompatible types Object Could not be converted [ArrayList]

Error: incompatible types Object Could not be converted [ArrayList]

我的 for 循环声明中出现编译器错误:incompatible types: Object cannot be converted to Type2

我正在尝试这样做:Java - List of objects. Find object(s) with a certain value in a field

代码如下:

import java.util.ArrayList;

public class Test2 {
    String name;
    int value;
    public ArrayList list = new ArrayList<Test2>();

    public void q() {
        for(Test2 w : list) { // Here is the error: 'incompatible types: Object cannot be converted to Test2'
            if(w.value == 10)
                System.out.println(w.name);
        }
    }
}

Java 无法知道在列表中期望 Test2 类型的对象;你需要参数化它。显式声明 listpublic ArrayList<Test2> list = new ArrayList<Test2>();,或在循环中转换它:(Test2 w : (ArrayList<Test2>)list).