• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pls explain what is method does

 
Mary Wallace
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This i saw in a book and I couldnt understand what exactly this method does.




any help appreciated
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it returns the index of a String object within a List object, if present.

Would the title of the book happen to be 'How Not to Write Java'?
 
Mary Wallace
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i pass 1 then the method returns 1. If I pass 2 the method return 2.
am i rt?
So what is the purpose of this method?
I think this method doesnt do anything.
Am i missing something

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing 1 and getting 1 is sort of a coincidence.

empindex.contains("1") - tells you whether "1" is in the vector

empindex.indexOf("1") - tells you what position it's in in the vector

This is silly code. A simple indexOf("1") would give you the same information without the invitation for disaster that comes with the magic number 99. What if the position of "1" really was position 99? This method would seem to tell you it was not found, which is wrong.

See the JavaDoc for Vector, read up on contains() and indexOf(). See if you can replace the body of this horror one line of code.

See the JavaDoc for the Collection interface and some of the classes in the "see also" section. See if you can avoid ever using Vector again.

Keep reading code and asking questions! Learning is fun!
[ November 03, 2004: Message edited by: Stan James ]
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ray,

Originally posted by Ray Stojonic:

Would the title of the book happen to be 'How Not to Write Java'?



Could you please expand a bit on this comment?
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramaswamy Srinivasan:
Could you please expand a bit on this comment?


Okay

the original code, with my comments:


All this code is after is the index of an item in the list, which hardly needs to be broken out into it's own method.

So, unless an author was trying to demonstrate bad coding practices, my guess is that this code had another source.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic