Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find value in string

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String se[]={"1","2" ,"3","4"};

may i know how to find if String se[] arry has vaule 3 in it?
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you don't want to use foreach, for loop you can convert this array to list and then use ".contains" method to detect the presence of desired string.
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] se, surely, not String se[].
 
Siddhesh Deodhar
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] se, surely, not String se[].


Both are valid. String[] se is simple to understand.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, there is no need convert to list and then search. another way is:

1. sort the array by using java.util.Arrays//this step is must since you are going for binary search

2.then use binarySearch of java.util.Arrays to find out( the index) the element(3) of the array

3.the binarySearch returns >=0 if and only if the key is found

hth
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddhesh Deodhar wrote:Both are valid. String[] se is simple to understand.


correct
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddhesh Deodhar wrote:Both are valid. String[] se is simple to understand.
String[] se is the recommended format, because the type of se is an array of Strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic