Solved

searching element in Array

Posted on 2008-06-24
8
236 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

18 Experts available now in Live!

Get 1:1 Help Now