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 + " ");
}
}
}
答案 0 :(得分:5)
你犯了一个简单的错误。您已调用next()然后调用nextLine(),因此打印从第二个元素开始。
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
答案 1 :(得分:0)
您不会将第一个读取元素添加到列表中:
String name = scanOne.next();
在这里读取一个int并将其作为所有其他列表添加到列表中。