Contact@itspring.com
  • Check Array Value in Java

    This post is for finding out, whether a particular value is present in an array or not.

    Array : Array is a collection of similar type of elements, stored along a contiguous memory location.

    Syntax

    dataType [] variable_name;

    e.g.

    int [] arr;

    This is an array of int dataType.

    Now, let's see the code to find a specific value in an array. Here code will check whether the value "Tony" is present in the given array of Strings or not.

    1.  Using for loop :

    This is the most simple method. Here a for loop will rotate over the array, in which a string named as 'str' will have the value that we wish to find from the array. If the value is found, message will be printed as value found.
     
    Here is the code :

    <p>&nbsp;/*Java program to find whether a particular string</p>
    <div>&nbsp;* is present in the array or not using for loop</div>
    <div>&nbsp;* */
    </div>
    <
    div>&nbsp;</div>
    <
    div>import java.util.Arrays;</div>
    <
    div>&nbsp;</div>
    <
    div>public class FindValueArray {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">    </span>public static void main(String[] args) {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// TODO Auto-generated method stub</div>
    <div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// array of string datatype&nbsp;</div>
    <div>&nbsp; &nbsp;<span style="white-space:pre">    </span>&nbsp; &nbsp; &nbsp; &nbspString[] names = new String[] { &quot;Bruce&quot;, &quot;Roger&quot;, &quot;Kent&quot;, &quot;Peter&quot;, &quot;Tony&quot; };</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>//for loop to find string</div>
    <div><span style="white-space:pre">        </span> for (String str names) {</div>
    <
    div><span style="white-space:pre">        </span>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span> if (str.equals(&quot;Tony&quot;))</div>
    <
    div><span style="white-space:pre">        </span>&nbsp;</div>
    <
    div><span style="white-space:pre">        </spanSystem.out.println(&quotTony is present in the array &quot;);</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div><span style="white-space:pre">        </span> }</div>
    <
    div><span style="white-space:pre">        </span></div>
    <
    div><span style="white-space:pre">    </span>}</div>
    <
    div>&nbsp;</div>
    <
    div>}</div>

Most Liked Posts

Post A Comment

Your email address will not be published. Required fields are marked*

LMKO

0 Comments

No Record found
demo content

The Total Economic Impactâ„¢ of ServiceNow

United States Tue Jun 06 2017

Join us for a webinar where we will reveal the quantified value of the Lightspeed Enterprise for customers that are modernizing IT, eliminating service outages, and optimizing performance with real-time analytics.

Guest speakers Liz Witherspoon of Forrester, and study participant Brad Paubel, VP of Internal Technologies at Maritz, will discuss

  • The value drivers for IT and key quantified economic benefits
  • Key challenges that study customers faced and overcame
  • Focus areas and best practices you can use to drive business impact with ServiceNow

Register

Webinar Registration

Enter the characters visible in the box below

6O64

Share

Share using

Advertise With Us