我正在尝试简单的链接列表收集程序,但它并没有让我放弃 我创造了这个东西
import java.lang.*;
import java.util.*;
class Linkedlist
{
public static void main (String [] args)
{
LinkedList ll=new LinkedList ();
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 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 litr=ll.listIterator ();
while (litr.hasNext ())
{
Object obj2=litr.next ();
System.out.print (obj2+",");
}
System.out.println ("\n");
while (litr.hasPrevious ())
{
Object obj3=litr.next ();
System.out.print (obj3+",");
}
System.out.println ("\n");
Object obj4=ll.get (2);// random retrieval
System.out.println (obj4);
}
}
输出
Note: Linkedlist.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
答案 0 :(得分:2)
我通过eclipse执行你的程序:
只使用LinkedList<Integer>
而不只是使用LinkedList。没有警告。
输出=&gt;
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)
答案 1 :(得分:1)
使用
LinkedList<Integer> ll = new LinkedList<Integer>();
Iterator<Integer> itr = ll.iterator();
ListIterator<Integer> litr = ll.listIterator();
而不是
LinkedList ll = new LinkedList();
Iterator itr = ll.iterator();
ListIterator litr = ll.listIterator();
如下图所示
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);
答案 2 :(得分:0)
第一个答案(由Nandkumar提供)是对的,我还检查了由这一行引起的运行时错误:
while (litr.hasPrevious ())
{
Object obj3=litr.next ();
System.out.print (obj3+",");
}
那就是:你检查你的迭代器是否有一个&#34;之前的&#34;项目,然后去下一个&#34;?可能不是你打算做的。
&#34;错误&#34;在编译时只是一个警告,你可以在编译后运行你的程序,但编译器告诉你它在你的代码中发现了一些奇怪的东西。