The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to test if an Array contains a value in Java - Linear Search

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to test if an Array contains a value in Java - Linear Search Posted: Nov 23, 2014 6:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to test if an Array contains a value in Java - Linear Search
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
One of the common coding question from Java interviews is how to test if an Array contains a certain value or not? This is simple question but some time interview pressure makes candidates nervous. Since array in Java doesn't have any inbuilt method for search, interviewer prefer to ask this question, to see how candidate deal with such situation. If you have good knowledge of Java API then you will immediately come to know that there are alternatives available e.g. binary search of Arrays class or taking advantage of ArrayList contains method by first converting your array to ArrayList. If you come up with those solution, Interviewer will surely ask you to write down a method to search an element in array without using any library method. You can easily solve this question if you know linear search or binary search algorithm. Linear search is very simple to implement, all you need to do is loop over array and check each value if that is the one or not. Binary search is little tricky but not too difficult either, recursive version is very natural as well. In this tutorial, though I have given two solution, one is using ArrayList, and second is using linear search, leaving binary search an exercise for you. But you must remember to sort array before using binary search. By the way to make question more challenging, I usually asked candidate to write a parametric method using generic so that it will work for any type of object array in Java.
Read more ยป

Read: How to test if an Array contains a value in Java - Linear Search

Topic: Swing and JavaFX: working with JFXPanel Previous Topic   Next Topic Topic: Building and Testing JSF.next

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use