开发者

How to increment array index

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开发者_如何学Go 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].


You can specify at what index the element must be added:

arr.add(index,st.nextToken());


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);


Do you really need to initalize the ArrayList size? Try your original code changing

ArrayList arr = new ArrayList(500);

for

ArrayList arr = new ArrayList();


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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜