Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

searching element in Array

Posted on 2008-06-24
8
Medium Priority
?
243 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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

618 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