import java.util.*;
public class Test
{
static List<Integer> list = new LinkedList<Integer>();
public static void main(String[] args)
{
Scanner scanOne = new Scanner(System.in);
System.out.println("Enter integers to be added to the list with a space between every number : ");
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
while (scanTwo.hasNextInt())
{
list.add(scanTwo.nextInt());
}
print();
}
public static void print()
{
ListIterator<Integer> listIT = list.listIterator(); // using the list Interface's method "listIterator()" to Iterate through the listIT
while(listIT.hasNext())
{
int n = listIT.next();
System.out.print(n + " ");
}
}
}
最佳答案
你犯了一个简单的错误。您调用了 next() 和 nextLine(),因此打印从第二个元素开始。
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
关于java - 我正在打印我的 linkedList 但第一个元素没有打印,这是为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546976/