Reputation: 2374
I am using arraylist in my java program but the problem is that when i add an item in arraylist its adding on the same index so i want to know that how to increment array index in array list.
ArrayList arr = new ArrayList(500);
StringTokenizer st = new StringTokenizer(line, ":Mode set - Out of Service In Service");
while(st.hasMoreTokens()){
arr.add(st.nextToken());
}
In above code its keep adding item on the same index i.e. arr[0].
Upvotes: 0
Views: 1535
Reputation: 471
Do you really need to initalize the ArrayList size? Try your original code changing
ArrayList arr = new ArrayList(500);
for
ArrayList arr = new ArrayList();
Upvotes: 0
Reputation: 424983
There's nothing wrong with your code. I don't believe that it would add to position zero.
Prove it by displaying the contents: System.out.println(arr);
Upvotes: 1
Reputation: 485
Your code works fine. I cant see any problem.
How do you know whats the value at index 0, I mean are you debugging it?
Is that text really your delimiter ?
Upvotes: 0
Reputation: 3192
You can specify at what index the element must be added:
arr.add(index,st.nextToken());
Upvotes: 1