Quote:
I need it to be at a certain position because each object must correspond to a certain position.
I'm no expert, but that says "Map" to me. (ie a map from key/position to value/question content).
Of course, as you say, the assignment asks you to use a list rather than a map... (If I were doing the assignment, I'd be confused enough to ask whoever assigned it, "why a list for something that's mapping positions to questions"?)
Quote:
I just really want to know why it is not allowing me to place at a position.
As the docs say, because the position lies outside the list's current size.
Quote:
I know that the location I'm trying to put it at is not negative so it must be that the location is bigger than the linkedlist size.
It might be worth verifying that. Ie printing (or toasting) both position and the list's size().
Quote:
How do I increase the size?
This is the heart of the matter. LinkedList (unlike, say, a JavaScript array or the olde fashioned Vector class) will not happily add null entries to make the array as big as you require at some point. Rather you have to do this yourself.
Check the API docs to make sure there is no method already there to do this. (As far as I know there isn't). Then write your own:
Code:
/**
* Makes sure a given list is at least a given size.
*/
private void ensureSize(List list, int size) {
// your code here
}
Basically your code will append nulls to the list until its size is at least as large as the int you pass.
[Edit] What you end up with is a list that may well have "holes" in it: ie null values. Other code that uses the list had better be prepared to cope with that.