user7544590
user7544590

Reputation: 59

Why does my Iterator not returning me elements from the entire linked list?

I am working on a HW question that asks: You are given a list, L , and a another list, P, containing integers sorted in ascending order. The operation printLots(L,P) will print the elements in L that are in positions specified by P. For instance, if P=1,3,4,6, the elements in positions 1,3,4, and 6 in L are printed. Write the procedure printLots(L,P).

I am only allowed to use methods from Collection which means I can't use get(index).

My code however, only returns me the last element in list P, not all. I don't know what is wrong.

Here is my code:

public static void printLots(Collection<Integer> L, Collection<Integer> P){

         int size = P.size();

        Iterator<Integer> iter = P.iterator();
        int pos = 0;
        while(iter.hasNext()){
            pos = iter.next();
        }
        System.out.println(pos);

        Integer [] arr = L.toArray(new Integer[size]);
        System.out.println(arr[pos]);


    }

    public static void main(String[] args){
        Collection<Integer> L = new ArrayList<>();
        Collection<Integer> P = new ArrayList<>();
        P.add(2);
        P.add(3);

        L.add(1);
        L.add(3);
        L.add(5);
        L.add(6);

        printLots(L,P);

    }

Upvotes: 0

Views: 1253

Answers (4)

IfOnly
IfOnly

Reputation: 540

public static void main(String[] args){
    Collection<Integer> L = new ArrayList<>();
    Collection<Integer> P = new ArrayList<>();
    P.add(0);
    P.add(1);
    P.add(3);
    P.add(4);


    L.add(1);
    L.add(3);
    L.add(5);
    L.add(6);

    printLots(L,P);

}

public static void printLots(Collection<Integer> L, Collection<Integer> P) {
    Integer[] arr = L.toArray(new Integer[L.size()]);

    Iterator<Integer> iter = P.iterator();
    int pos = 0;
    while (iter.hasNext()) {
        pos = iter.next();

        if (pos >= arr.length) {
            System.out.println("Position[P] : " + pos + "    " + "Location[L] : OUT OF INDEX.");
        } else {
            System.out.println("Position[P] : " + pos + "    " + "Location[L] : " + arr[pos]);                
        }
    }
}

Upvotes: 0

heshik nandan
heshik nandan

Reputation: 21

Try this method :

public static void printLots(Collection<Integer> L, Collection<Integer> P) {
        ArrayList<Integer> arrayListL = (ArrayList<Integer>) L;\
        System.out.println(arrayListL.stream().filter(x ->  P.contains(arrayListL.indexOf(x))).collect(Collectors.toList()).toString());
    }

What it does :

  1. Take every element inside L, check if the indexOf() is inside P.
  2. If has, then filter that element and add it to the collection.
  3. Print Collection

Reason why converting Collection L to ArrayList, is because Collection doesn't have a defined index.

Upvotes: 0

AxelH
AxelH

Reputation: 14572

Here is your problem :

int pos = 0;
while(iter.hasNext()){
    pos = iter.next();
}
System.out.println(pos);

Since you do nothing in the loop, you end up with pos being the last value.

int pos = 0;
while(iter.hasNext()){
    pos = iter.next();
    System.out.println(pos);
}

Will show you the full list.

PS: since I am not sure I undestand what you want to do, I will only point out this problem ;) you build an array from the original list. I guess you want to use contains to check if the value exist in the other list and add it to an Array/List to print the result.

Upvotes: 1

newday
newday

Reputation: 3878

change your code to

while(iter.hasNext()){
     pos = iter.next();
     System.out.println(pos);
}

Upvotes: 0

Related Questions