• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching a Vector element for one or more specific number ??

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one small question. I can't figure out how to search a vector element for one or more numbers (that the user inputs). Can't be that hard I recon, just can't figure out how. Anyone ?
I tried using: if (v.contains(1)) but that gives me an Incompatible type for method error ..
Well, gotta sleep soon Too much Java left though
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't store numbers in a vector unless they are wrapped in an object.
 
Fredrik Komstadius
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are wrapped in an object.... So the question might be how I should search the object for the numbers
[ May 18, 2002: Message edited by: Fredrik Komstadius ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're close with your original attempt. Just as you must wrap a number with an object to store it in a Vector or other Collection, you must wrap a number the same way to search a Collection for that number. Assuming you're using Integer as the wrapping class:
v.contains(new Integer(1))
will report if v contains an Integer with value 1.
Incidentally, if the Vector is large, a HashSet or TreeSet would be more efficient than Vector. Might be worth considering...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Extrapolating from your previous thread, are you maybe wanting to search for the value of a specific field of the objects in your Vector? Should this be the case, then you'll need to iterate through each component in the Vector and test each object's specific field for equality.
A further description of the strategy I'm proposing:

Are you getting any ideas yet?
 
Fredrik Komstadius
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, thanx guys! You really helped me out here! Very much Appreciated, thanx
 
Fredrik Komstadius
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, now I have tested that code you suggested and it seems to work fine, but I don't understant what you mean with:
/* use and/or return the index or object */ ?
What does this mean, I want to search each part of my vector for number and then if they are found contine..
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does this mean, I want to search each part of my vector for number and then if they are found contine..
That's more or less what I meant.
 
Fredrik Komstadius
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Feel kind of stupid but I just wonder what do to inside the fieldEqual() ?
I.E: I can't write: MyObject.fieldEqual(NUMBER) {prinln ("test"))....
I get this error:
method fieldEquals() not found in class java.lang.Object
[ May 19, 2002: Message edited by: Fredrik Komstadius ]
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fredrik, I have not heard of fieldequal(). Is it new? Did you mean the equals() method?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bosun, as Fredrik's previous post suggests, the fieldEquals method does not exist. I used it in the example I posted further up the page.
Fredrik, you'll need to create whatever method you wish to use to check for equality of the appropriate field(s) in your class that defines the objects you're using. Obviously, you can name this method anything you want.
[ May 20, 2002: Message edited by: Dirk Schreckmann ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic