[英]Java complains about incompatible types when using same type
I tried looking around some around here and on Google, but unfortunatly found nothing. 我尝试在这里和Google上四处寻找,但很遗憾,没有发现任何东西。
Here is my code: 这是我的代码:
class DeleteLists
{
public static void main(String[] args)
{
TokenAuth auth = new TokenAuth("xxxxx", "xxxxx");
JKippt jkippt = new JKippt(auth);
ClipList[] lists = jkippt.getLists();
for (int i = 0; i <= lists.length - 1; i++)
{
if (lists[i].isPrivate() == false);
{
{System.out.println(lists[i].getTitle);
jkippt.deleteList(lists[i].getId());
}
}
}
}
When I try compiling it, it outputs this: 当我尝试编译它时,它输出以下内容:
DeleteLists.java:18: error: incompatible types
ClipList[] lists = jkippt.getLists();
^
required: ClipList[]
found: Iterator<ClipList>
This could very possibly be a very basic mistake, but what am I doing wrong here? 这很可能是一个非常基本的错误,但是我在这里做错了什么?
Thank you very much in advance! 提前非常感谢您! Eden.
伊甸园
This method returns an Iterator
: 此方法返回一个
Iterator
:
Iterator<ClipList> it = jkippt.getLists();
while (it.hasNext()) {
ClipList clipList = it.next();
// do stuff with clipList
}
Deciding to return an Iterator
from an API method (particularly one named getLists()
) is a strange decision, it would be better if it returned a List
or a Collection
(or at least a Iterable
). 决定从API方法(尤其是一个名为
getLists()
)返回Iterator
是一个奇怪的决定,如果它返回List
或Collection
(或至少是Iterable
),那就更好了。 If that where the case, you could use it like this: 如果是这样,您可以这样使用它:
for (ClipList clipList : jkippt.getLists()) {
// do stuff
}
The getLists()
method returns an Iterator
of ClipList
elements. getLists()
方法返回一个ClipList
元素的Iterator
。 There are more examples in the library project page: https://code.google.com/p/jkippt/ 图书馆项目页面中还有更多示例: https : //code.google.com/p/jkippt/
Disclaimer: I'm the author of the library. 免责声明:我是图书馆的作者。
The method returns an Iterator, not an array. 该方法返回一个Iterator,而不是数组。 To go over the elements of an iterator you can use the while loop:
要遍历迭代器的元素,可以使用while循环:
Iterator<ClipList> iter = jkippt.getLists();
while (iter.hasNext()) {
ClipList list = iter.next();
...
}
You have to use an Iterator
loop: 您必须使用
Iterator
循环:
for (Iterator<ClipList> i = jkippt.getLists(); i.hasNext(); ) {
ClipList list = i.next();
if (!list.isPrivate()) {
System.out.println(list.getTitle);
i.remove();
}
}
Note that you have a bug in your code - there is an errant semicolon after your if
, which has the effect of making the if
statement have an empty block. 请注意,您有一个错误在你的代码-有一个错误的分号你之后
if
,它具有使的效果if
语句有一个空块。
Also, this line of yours will probably throw an ConcurrentModificationException
: 另外,您的这一行可能会抛出
ConcurrentModificationException
:
jkippt.deleteList(list.getId());
so I changed it to the proper way to delete using an iterator via the remove()
method. 所以我将其更改为通过
remove()
方法使用迭代器删除的正确方法。
Also note the change here to using if (!list.isPrivate())
rather than your if (list.isPrivate() == false)
, which is unnecessarily cluttered. 还要注意这里使用
if (!list.isPrivate())
而不是您的if (list.isPrivate() == false)
,这是不必要的。
As said, getLists() returns an iterator. 如前所述,getLists()返回一个迭代器。 You can convert it to an arraylist with the following code sample:
您可以使用以下代码示例将其转换为数组列表:
Iterator<String> lists = jkippt.getLists();
List<String> result = new ArrayList<String>();
while (list.hasNext())
result.add(list.next());
Here result
is your arraylist. result
是您的arraylist。 If you want to convert it to an array I suggest you visit the topic ArrayList to array 如果要将其转换为数组,建议您访问主题ArrayList to array
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.