This week's book giveaway is in the Java in General forum.
We're giving away four copies of Java by Comparison (eBook) and have Simon Harrer, Jörg Lenhard, Linus Dietz on-line!
See this thread for details.
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

find value in string  RSS feed

 
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?
 
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.
 
Sheriff
Posts: 57782
178
  • 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.
 
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: 57782
178
  • 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
Boost this thread!