Search code examples
javaarrayslistlistiterator

How can I convert an array into an ListIterator?


I have an array of reference type. I want to use hasPrevious() and hasNext() methods. I want to use ListIterator. Is it the right way to convert an array into an ListIterator?


Solution

  • The easiest way I found to do this is to turn the array into a List. The following code is using way of hasPrevious()

    Object[] objArray = { new Object(), new Object() };
    ListIterator<Object> listIterator = Arrays.asList(objArray).listIterator(objArray.length);
    while (listIterator.hasPrevious()) {
        Object object = listIterator.previous();
        System.out.println(object.toString());
    }