?
Solved

searching element in Array

Posted on 2008-06-24
8
Medium Priority
?
241 Views
Last Modified: 2010-03-30
hi guys
i have an array or codes like this
String[] codes =[a,b,c,z] .

i want to find if the above array contains the element b,
Is there a contains() method with arrays?

any idea how i can find out if the array contains element 'b'?

I am assuming for loop , but not sure how.

for(int i =0;i<codes.length;i++)
{
any idea?
}

thanks
J
0
Comment
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Assisted Solution

by:RishadanPort
RishadanPort earned 80 total points
ID: 21859605
for(int index = 0; index < codes.length; index++)
{
   if(codes[index] == b){
      <Do something>
   }
}
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21859619
What do you mean the "element" 'b'. Do you mean the character b?

This is a String Array, so the array will contain Strings, not individual characters. 1 single character IE if 'b', will actually be 1 string "b".
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21859650
If you're trying to Count the occurences of the character 'b' in the entire array, you can do something like this:

int occurences = 0;

for(int index = 0; index < codes.length; index++){
   for(int index2 = 0; index2 < codes[index].length; index2++){
      if(codes[index].charAt(index2) == 'b'){
         occurences++;
      }
   }
}

System.out.println("The total occurences is " + occurences);
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 24

Accepted Solution

by:
sciuriware earned 1600 total points
ID: 21859849
First of all your syntax is wrong:

      String[] codes =[a,b,c,z];

should be:

      String[] codes = new String[]{"a","b","c","z"};

Then, arrays lack  the contains() method because those are NOT collections.

Every Collection derived class in JAVA has what you want, so convert the array to a collection, like:

      String[] codes = new String[]{"a","b","c","z"};

      ArrayList<String> h = new ArrayList<String>(Arrays.asList(codes));
     
      if(h.contains("c"))
      {
         // Do something.
      }

;JOOP!
0
 
LVL 10

Author Comment

by:jaggernat
ID: 21860108
ok, thnks guys, i think i got what i wanted
0
 
LVL 4

Assisted Solution

by:aman123_123
aman123_123 earned 120 total points
ID: 21862749
The correct way of doing this is by using the utility class Arrays.
Here is the code


                     String searchString[] = new String[] {"a", "b",  "c"};
      Arrays.sort(searchString);
      int index = Arrays.binarySearch(searchString, "b");
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 21862883
No, it is A way of doing this, with the advantage that it is slightly faster
when a large number of 'look-ups' must be done.
The utility class Arrays, which I deployed too, has some interesting tools and converters.

Any way the best answer to the original question depends on the final purpose
of the 'contains' question. In some cases data must be removed or added;
then an array is a bad place to keep the data.

;JOOP!
0
 
LVL 10

Author Comment

by:jaggernat
ID: 21868163
thanks guys

any help with my next question greatly apprciated
http://www.experts-exchange.com/Programming/Languages/Java/Q_23515494.html
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question