• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How do I search this Vector or String ?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have a vector j that I need to search for one or more numbers. The program shall when it finds the number return some kind of value so that I can print out a element in another vector v... I can't figure out how to search the Vector.. I have converted the Vector elements I need into a String but that doesn't seem to make sense. Maybe if I converted the elements into ints or something? Well, I'm very much a newbie and very much lost... Help would be appreciated
/Bossakungen
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bossakungen -
Please change your name to conform to JavaRanch's naming policy.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand you correctly, you need to search specific numbers in a Vector.
What vector contains are Objects. Try this,
For(Enumeration e = v.elements; e.hasMoreElements(); ) {
Integer i = (Integer) v.nextElement();
if(i.intValue() == numUWant)
doWhatUWant...;
continue;
}
}
-yc
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The class java.util.Collections (not to be confused with the java.util.Collection interface) has a static method binarySearch(List list, Object key). It returns an int which is the index of the matching Object in the Vector.
-anthony
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Collections.binarySearch() method is only useful if the Collection (i.e. the Vector in this case) has already been sorted - whether by Collections.sort(), or some other process which has ensured that the elements of the Collection are in order.
[ March 29, 2002: Message edited by: Jim Yingst ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Ah yes, you are correct in pointing that out.
-anthony
 
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
Thanx guys! Really appreciate it! Sorry about that name thing, changed now
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic