英文:
Returning com.badlogic.gdx.utils.Array<> of interfaces in Java
问题
我尝试声明方法:
public Array<Shape2D> getShapes(){
...
}
然后我收到编译器错误,指出在圆括号前缺少';'。而且在getShapes
内部的return
语句也被下划线标记,并显示提示“无法从void
返回类型的方法返回值”。我不得不说我是Java的新手,但我认为它会工作。我不能返回Array<>
类型吗?
英文:
I tried to declare method:
public Array<Shape2D> getShapes(){
...
}
And I got compiler error, that ';' is missing before round brackets. And return
statement inside getShapes
also is underlined with hint "Cannot return a value from method of void return type". I have to say I'm new in Java, but I assumed it would work. Can't I return Array<> type?
答案1
得分: 1
在Java中,Array.class
没有类型参数。
您可以使用Shape2D[]
,这是一个特定类型的实际数组。
如果您的编译器出现与void返回有关的问题,可能是因为Array<Shape2D>
不是声明数组的方式。因此,它会认为它是一个void类型的方法。
在这种情况下,您的代码应该像这样:
public Shape2D[] getShapes(){
...
}
如果Array.class
不是下面提到的那个类,则可能导入了其他内容。
提到的类:
java.lang.reflect.Array
英文:
In Java, the Array.class
does not have type parameters.
What you can do is Shape2D[]
instead which is an actual array of this specific type.
If your compiler sees a problem involving the void return, it's probably because Array<Shape2D>
isn't the way you declare an array. Therefore, it thinks it's a void type method.
In this case, your code needs to look like this:
public Shape2D[] getShapes(){
...
}
If the Array.class is not the one mentioned below, then you may have imported something else.
Mention class:
java.lang.reflect.Array
答案2
得分: 0
抱歉,我之前没有看到括号,尝试在另一个方法内声明方法。对不起浪费了您的时间。
英文:
I didn't see the brackets and tried to declare method inside other method. Sorry for wasting your time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论