Java question, please help?

2012-11-26 10:52 am
String b = "b";
Vector a = new Vector(10);
a.add(5, b);

I just want to add b to the position 5 at the vector, but how come I got an Indexoutofbound error?
How can I fix this? Thanks.

回答 (1)

2012-11-26 11:33 am
✔ 最佳答案
Because when adding elements to a Vector you have to start adding at the index 0
For example
Vector<String> a = new Vector<String>(10);
a.add(0, "a");
a.add(1, "b");
a.add(3, "c");
would throw the same exception at the line 4. I suggest using an array instead of a vector
參考: Personal knowledge


收錄日期: 2021-05-01 14:17:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20121126025212AAQ209b

檢視 Wayback Machine 備份