-1

I tried looking around some around here and on Google, but unfortunatly found nothing.

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.

Eden Landau
  • 553
  • 4
  • 12

5 Answers5

8

This method returns an 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). If that where the case, you could use it like this:

for (ClipList clipList : jkippt.getLists()) {
  // do stuff
}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I've used the Iterator because the service is returning back a paged result. In the background the ClipList iterator iterate over an iterator of result pages. Returning a List or a Collection was not possible unless getting all the result pages in one method call. The Iterable could be a solution but this solution can hide resource allocation to the user (requiring result page through http). The method name is from the Kippt service endpoint, I admit it is ugly . – Fedy2 Jul 11 '13 at 19:14
1

The getLists() method returns an Iterator of ClipList elements. There are more examples in the library project page: https://code.google.com/p/jkippt/

Disclaimer: I'm the author of the library.

Fedy2
  • 3,147
  • 4
  • 26
  • 44
0

The method returns an Iterator, not an array. To go over the elements of an iterator you can use the while loop:

Iterator<ClipList> iter = jkippt.getLists();
while (iter.hasNext()) {
    ClipList list = iter.next();
    ...
}
Joni
  • 108,737
  • 14
  • 143
  • 193
0

You have to use an Iterator loop:

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.

Also, this line of yours will probably throw an ConcurrentModificationException:

jkippt.deleteList(list.getId());

so I changed it to the proper way to delete using an iterator via the remove() method.

Also note the change here to using if (!list.isPrivate()) rather than your if (list.isPrivate() == false), which is unnecessarily cluttered.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

As said, getLists() returns an iterator. 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. If you want to convert it to an array I suggest you visit the topic ArrayList to array

Community
  • 1
  • 1
Joren
  • 3,068
  • 25
  • 44