trying to implement concept of LinkedList through java , but it is giving giving error at compile time?

前端 未结 3 1294
故里飘歌
故里飘歌 2021-01-24 21:52

I am trying simple linked list Collection program, but it is not giving me out put.
i created this stuff

import java.lang.*;  
import java.util.*;  

cla         


        
相关标签:
3条回答
  • 2021-01-24 21:58

    Use

    LinkedList<Integer> ll = new LinkedList<Integer>();
    Iterator<Integer> itr = ll.iterator();
    ListIterator<Integer> litr = ll.listIterator();
    

    Insted of

    LinkedList ll = new LinkedList();
    Iterator itr = ll.iterator();
    ListIterator litr = ll.listIterator();
    

    As shown below

        LinkedList<Integer> ll = new LinkedList<Integer>();
        System.out.println("CONTENTS OF l1 = " + ll);
        System.out.println("SIZE = " + ll.size());
        ll.add(new Integer(10));
        ll.add(new Integer(20));
        ll.add(new Integer(30));
        ll.add(new Integer(40));
        System.out.println("CONTENTS OF ll = " + ll);
        System.out.println("SIZE = " + ll.size());
        // retrieving data of ll using toArray ()
        Object obj[] = ll.toArray();
        int s = 0;
        for (int i = 0; i < obj.length; i++) {
            Integer io = (Integer) obj[i];
            int x = io.intValue();
            s = s + x;
        }
        System.out.println("SUM USING toArray () = " + s);
        ll.addFirst(new Integer(5));
        ll.addFirst(new Integer(6));
        System.out.println("CONTENTS OF ll = " + ll);
        System.out.println("SIZE = " + ll.size());
        // retrieving data of ll using iterator ()
        Iterator<Integer> itr = ll.iterator();
        int s1 = 0;
        while (itr.hasNext()) {
            Object obj1 = itr.next();
            Integer io1 = (Integer) obj1;
            int x1 = io1.intValue();
            s1 = s1 + x1;
        }
        System.out.println("SUM USING iterator () = " + s1);
        ListIterator<Integer> litr = ll.listIterator();
        while (litr.hasNext()) {
            Object obj2 = litr.next();
            System.out.print(obj2 + ",");
        }
        System.out.println("\n");
        while (litr.hasNext()) {
            Object obj3 = litr.next();
            System.out.print(obj3 + ",");
        }
        System.out.println("\n");
        Object obj4 = ll.get(2);// random retrieval
        System.out.println(obj4);
    
    0 讨论(0)
  • 2021-01-24 22:11

    The first answer (by Nandkumar) is right, I'd also check the runtime error, caused by this lines:

    while (litr.hasPrevious ())  
        {  
            Object obj3=litr.next ();  
            System.out.print (obj3+",");  
        }
    

    That is: you check if your iterator has a "previous" item, then go for tne "next"? Probably not what you intended to do.

    The "error" at compile time is only a warning, you can run your program after compilation, but the compiler is telling you that it found something strange in your code.

    0 讨论(0)
  • 2021-01-24 22:15

    I executed your program through eclipse :

    Just use LinkedList<Integer> instead of just LinkedList. No Warnings.

    Output =>

    CONTENTS OF l1 = []
    SIZE = 0
    CONTENTS OF ll = [10, 20, 30, 40]
    SIZE = 4
    SUM USING toArray () = 100
    CONTENTS OF ll = [6, 5, 10, 20, 30, 40]
    SIZE = 6
    SUM USING iterator () = 111
    6,5,10,20,30,40,
    
    Exception in thread "main" java.util.NoSuchElementException
        at java.util.LinkedList$ListItr.next(Unknown Source)
        at com.Linkedlist.main(Test.java:46)
    
    0 讨论(0)
提交回复
热议问题