Solved

searching element in Array

Posted on 2008-06-24
8
237 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 85
Checkbox and ListView in Android Layout 4 56
github account with ecipse 1 42
how to add new optional parameter to JSP 1 29
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now