Reputation: 77
For example i want to add the 1 object to the first index, then i want to add a another object to index 5. So i haven't added anything to 2,3,4,5. Is this possible?
The main reason i want to do this is because i have 2 kinds of activities. I want to store the first kind of activity on the list from 1-5 and the second from 6-10. So this way when i ask the user for what kind of activity she is looking for (first type or second type) i will know which part of the array list to start looking at.
Upvotes: 3
Views: 8617
Reputation: 401
You can perform this only by first initializing the list with dummy value and then later you can replace its different indexes according to you wish.
For eg: ArrayList strList = new ArrayList();
If your lists size is always constant, say 6, then first initialize it with some dummy values as [ , , , , , ] Now you can use the range from the size for "first type" and "second type". Lets take 3 each. You can set each range by using set:
strList.set(0, "first type 1");
strList.set(4, "second type 2");
To be frank, instead of going for this approach, the best would be to use two separate lists for different activities.
Upvotes: 0
Reputation: 37845
Not normally, no, because this is not how the List abstraction works. A List has a variable size and its size is equal to the number of elements it contains. It provides access by numerical index, but only to the indices of its existing elements.
You can do this yourself by padding with null
, for example:
public static void ensureSize(List<?> inList, int numIndices) {
if(inList.size() >= numIndices) {
return;
}
for(; numIndices > 0; --numIndices) {
inList.add(null);
}
}
Be aware that with this approach that:
null
elements until they are set
to something else (as would an array).add
and remove
on a List causes the indexing of the elements to change. Make sure you are appropriately using set
and get
.However, if the size of your List is known ahead of time and fixed, it may very well be appropriate to simply use an array.
If you just want to go through the List interface because you find it easy to work with, you might also initialize it with Arrays#asList
:
// ( java.util.Arrays )
List<String> fixedList = Arrays.asList(new String[n]);
This creates a List with a fixed size that wraps around the array. You can set
and get
on it but not add
and remove
. (Calling add
, remove
and other methods that attempt to alter the size will throw an exception.)
Upvotes: 1
Reputation: 17577
No it is not possible. You'll get an IndexOutOfBoundsException
if you try to add something to an index that exceeds the current list size.
But you can initialize your list with an "empty" instance of your list type (or null
if you take care) first and use set
instead of add
:
public static void main(String[] args) {
List<String> l = initList();
l.set(0, "activity 1");
l.set(5, "activity 2");
System.out.println(l);
}
private static List<String> initList() {
final List<String> l = new ArrayList<>();
for (int i = 0; i < 10; i++) {
l.add(""); // or null, but this can cause some problems ... (try to find an empty representation for an "activity" instance)
}
return l;
}
Output:
[activity 1, null, null, null, null, activity 2, null, null, null, null]
It is necessary to use set
instead of add
, or you're increasing the size of the list with every add
call. This will shift current entries on higher indices (element on index 6
will then be on index 7
if you add someting on an index below 6
).
So you should think about your current concept. Maybe it is easier to use two lists or an array instead.
Upvotes: 3
Reputation: 23226
If you really want to do this then see Apache Commons Collections Growth list
Upvotes: 1
Reputation: 85779
For an ArrayList
, this is possible only if the random index is between 0 and yourArrayList.size()
. Otherwise, you will get IndexOutOfBoundsException
. This is clearly stated at javadoc of ArrayList#add(int, E)
.
For your case, it would be better to use plain array rather than an ArrayList.
Upvotes: 0