The construct
List<Integer> booksList = new LinkedList<>();
is often seen for mainly 2 reasons:
- a) You aren't sure what the best collection for your purpose is. There are many candidates: Arrays, Sets, Lists, Vector, Map and so on, but you have to use something specific. So on the right hand side it has to be concrete, but on the left hand side, you try to be as wide open as possible.
Very often you just do iterate over the collection, use a for loop, search, sort, put into, take from, delete.
If you find out, that a sibling of your first thought is better suited, you can later change the RHS, without need to rewrite the rest of your program, because, for example, most methods of LinkedList and ArrayList fullfill the same contract, defined in List.
- b) You get mor compatible to the outside world, if you use the wider object. Of course, you can't use something as vague as Object, since Object has no .add (E) Method.
But for sorting, searching and so on List is well suited. Have a look at the documentation, and compare List, LinkedList and ArrayList. Try to replace the one with another in your code.
Maybe two 'sublist (int fromIndex, int toIndex)' methods, in Linked~ and ArrayList are defined in the base class, and behave identically.
But maybe they are specialized, and implement subList on their own. Here we have a Interface <- Class relationship, so we know, that there is no implementation in the Interface, it is completly abstract. But in the case of (Abstract) base classes <- derived classes the pattern is the same.
Derived/implementing classes share the same interface, 'public List sublist (int fromIndex, int toIndex)'. You expect them to produce the same result, but in different ways - maybe.
Only if you need to call a method which is only present in, say LinkedList, you either declare your bookList as LinkedList up front, or you cast it for that purpose:
List<Integer> books = new LinkedList<>();
// ...
LinkedList <Integer> booksLiLi = (LinkedList <Integer>) books <> ();
// do something with booksLiLi which isn't defined on the List interface.
Sidenote: Your code can be simplified with the modern (7 years old) foreach-loop:
Scanner input = new Scanner (System.in);
LinkedList<Integer> booksList = new LinkedList<>();
System.out.printf ("Give: ");
String N = input.nextLine ();
String[] arr = answer.split (" ");
for (String num: arr) {
booksList.add (Integer.parseInt (num));
}
bookList.remove ();
bookList.remove ();
But if you declare a Scanner, you can take Integers from the Scanner directly:
Scanner input = new Scanner (System.in);
LinkedList<Integer> booksList = new LinkedList<>();
System.out.printf ("Give: ");
while (input.hasNextInt ()) {
booksList.add (input.nextInt ());
}
bookList.remove ();
bookList.remove ();
In reaction to the comments, here is a third code: Reading from System.in a line, and then creating a Scanner on that that line:
String line = input.nextLine ();
Scanner valueLine = new Scanner (line);
List<Integer> books = new LinkedList<>();
while (valueLine.hasNextInt ()) {
books.add (valueLine.nextInt ());
}