• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector.add(int i,Object obj)  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,
i am working with Vector class to add some elements.
so i am using this syntax
Vector v=new Vector();

v.add("xyz");
v.add("abc");

and when i referred to the java documentation there was no method with add(Object obj);

but there is a method called add(int i, Obj o);

when i use add(Obj o); the java compiler doesn't give error saying that undefined method.??
The same thing happens with ArrayList
can any one expain me why this happens
thanks in advance
cinux


:roll:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

... when i referred to the java documentation there was no method with add(Object obj);


Vector does have a method that does this. In the java 1.5 API docs the signiture is different because Coleections use parameterized types, but for a raw Vector this is legal.


public boolean add(E o)

Appends the specified element to the end of this Vector.
 
Kia Phia Ben
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see you JDK and classpath. I using JDK1.4.2 and notepad. No problem.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
??? is this really a question for the advanced java forum ???

there are 3 different stages to choose from, and your post is - excuse me - a 100% beginner question about the usage of one of the most common java objects.

having said this, did you take into account that using the java.util.Vector gives you all the overhead / benefit of synchronized access? are you sure you want this? you might want to check out ArrayList, as this is not synchronized...

many greetings,
jan
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u r not getting my problem clearly..
ok let me explain it more clearly..
In ArrayList class there is no method like void add(object);
but we have thid method to ad our elements to a ArraList
add(int i, Object o);
so, by using the above mthod add("sdasd");
it takes with out giving me any compile time error?
how this possible..


I want the solution plzzz help out..
thanx in advance
cinux
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want the solution plzzz help out..
thanx in advance
cinux


Just went and reconfirmed the Javadocs on the sun site. The add() method that takes a single object is definitely mentioned.

Slight issues though. First, the return type is boolean -- not void. Second, for Java 5, the generic syntax is used, so it is listed as add(E o) instead of add(Object o).

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jan Groth:
??? is this really a question for the advanced java forum ???


No, sure isn't. It's going to the Java in General (Beginner) forum now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!