Solved

searching element in Array

Posted on 2008-06-24
8
238 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Assisted Solution

by:RishadanPort
RishadanPort earned 20 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 24

Accepted Solution

by:
sciuriware earned 400 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 30 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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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