[英]Eclipse and class import issues
I am experiencing an odd behaviour using Eclipse: I am trying to use an ArrayList
so if I try to import the class and then empoy it, I get a nasty error. 我在使用Eclipse时遇到一种奇怪的行为:我试图使用
ArrayList
所以如果我尝试导入该类然后使用它,则会收到一个讨厌的错误。
//... bunch of other imports
//Warning: unused import
import java.util.ArrayList;
public class TagHandler {
public static <ArrayList>Tag getRows(int id) throws SQLException {
String query = "SELECT * FROM Tag WHERE id = ?";
//Unexpected type error
ArrayList<Tag> list = null;
ResultSet rs = null;
// ... more code ...
}
but if I instantiate an object using the absolute path of the package, Eclipse is ok with that. 但是,如果我使用包的绝对路径实例化一个对象,那么Eclipse就可以了。
//... bunch of other imports
public class TagHandler {
public static <ArrayList>Tag getRows(int id) throws SQLException {
String query = "SELECT * FROM Tag WHERE id = ?";
//Works fine
java.util.ArrayList<Tag> list = null;
ResultSet rs = null;
// ... more code ...
}
I have written other classes and I have always imported the ArrayList
class and used it without a hitch, so what could possibly be wrong? 我已经编写了其他类,并且始终导入
ArrayList
类,并且毫不费力地使用了它,那么可能是什么错误呢?
You are declaring a generic type parameter for your method that shadows the ArrayList
type 您正在为您的方法声明一个通用类型参数,该参数会遮蔽
ArrayList
类型
public static <ArrayList> Tag getRows(int id) throws SQLException {
^
If you use the simple name ArrayList
, you are referring to that type variable, not the actual java.util.ArrayList
type. 如果使用简单名称
ArrayList
,则引用的是该类型变量,而不是实际的java.util.ArrayList
类型。
Your method was equivalent to 您的方法等效于
public static <T> Tag getRows(int id) throws SQLException {
String query = "SELECT * FROM Tag WHERE id = ?";
//Unexpected type error
T<Tag> list = null;
ResultSet rs = null;
which is not syntactically correct. 这在语法上是不正确的。 You probably meant
ArrayList<Tag>
instead of <ArrayList>Tag
. 您可能是指
ArrayList<Tag>
而不是<ArrayList>Tag
。
That is because the declaration of your static method is incorrect 那是因为您的静态方法的声明不正确
public static <ArrayList>Tag getRows(int id) throws SQLException
should be 应该
public static ArrayList<Tag> getRows(int id) throws SQLException
try fixing your static method and importing ArrayList
with an import at the top of the file 尝试修复您的静态方法并使用文件顶部的导入导入
ArrayList
import java.util.ArrayList;
public class TagHandler {
public static ArrayList<Tag> getRows(int id) throws SQLException {
String query = "SELECT * FROM Tag WHERE id = ?";
ArrayList<Tag> list = null;
ResultSet rs = null;
// ... more code ...
}
应该
public static ArrayList<Tag> getRows(int id) throws SQLException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.