简体   繁体   中英

Can I use listiterator in Java to access elements in random positions?

I'm writing code that goes through article records (in database). I load the database in memory and store it in ListIterator .

Does anyone know if I can access elements in random positions?

I created this Java example:

package com.myjava.listiterator;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class MyListIterator {
    public static void main(String a[]){
        List<Integer> li = new ArrayList<Integer>();
        ListIterator<Integer> litr = null;
        li.add(23);
        li.add(98);
        li.add(29);
        li.add(71);
        li.add(5);
        litr=li.listIterator();
        System.out.println("Elements in forward directiton");
        while(litr.hasNext()){
            System.out.println(litr.next());
        }
        System.out.println("Elements in backward directiton");
        while(litr.hasPrevious()){
            System.out.println(litr.previous());
        }

        // How to access litr[3]?
    }
}

This code loops through numbers forward and backward. See my last comment. Can I access litr[3] ?

// How to access litr[3]?

No you can't get randomly from an iterator.

From ListIterator docs

A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

// How to access litr[3]?

I can see a possibility here by getting it from list directly li.get(3);

If you don't have a list in hand and only have iterater, first prepare a list

List<ObjectType> dupList= new ArrayList<ObjectType>();
while (itr.hasNext())
    dupList.add(itr.next());

Alternative if you have to get only one object:

ObjectType target =null;
int i = 0;
while(itr.hasNext() && i!=3){
    i++;
    target = itr.next();
}
//access fourth element via target variable.

Now pass a random index to this list (dupList)

Your list instance is an ArryaList, simply use it directly.

package com.myjava.listiterator;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class MyListIterator {
    public static void main(String a[]){
        ArrayList<Integer> li = new ArrayList<Integer>();
        li.add(23);
        li.add(98);
        li.add(29);
        li.add(71);
        li.add(5);

        ....

        // How to access litr[3]?
        System.out.println(li.get(3));
    }
}

If your method only accept ListIterator and cannot be modified. Here is a solution if you are not concerned about performance.

package com.myjava.listiterator;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class MyListIterator {

    public void someMethodOnlyAcceptListIterator(ListIterator<Integer> iterator){
        List<Integer> tmpList = new ArrayList<Integer>();

        // Save all items to the tmpList
        while(iterator.hasNext()){
            tmpList.add(litr.next());
        }

        // Access item
        System.out.println(tmpList.get(3));
    }
}

you can directly use like this li.get(3)

you can also do like this but not good.

while(litr.hasNext()){
    int curentIndex = litr.nextIndex()-1;
    if(ranIndex > currentIndex) {
         while(litr.hasNext()){
            if(ranIndex  == litr.nextIndex()-1)
               sysout(yourrandomindex);
               after that reset back the list curentIndex 
        }

    }
    // less than code here
}

There is no such functionality in ListIterator . ListIterator allows for forward and backward iterations.

As for your solution to get litr[3] , you can take your ListIterator and apply all its elements into an ArrayList and do list.get(3) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM