我打印出我的链接列表但第一个元素不打印为什么会这样

WM.*_*WM. 1 java

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 + " ");
        }
    }




}
Run Code Online (Sandbox Code Playgroud)

atx*_*atx 5

你犯了一个简单的错误.您调用了next()然后调用nextLine(),因此打印从第二个元素开始.

    String name = scanOne.next();
    Scanner scanTwo = new Scanner(scanOne.nextLine());
Run Code Online (Sandbox Code Playgroud)