Store all the elements in an Array Randomly and search in Java Data Structures

In this program we will store all the elements in array randomly and then we will search any element from them. If exist then return true else false..

Code
public class SearchArray {
    protected int[] values;
    SearchArray(int size){
        values = new int[size];
        for (int i=0; i<size; i++){
            int randomNumber = (int)(Math.random()*size)+1;
            System.out.println(randomNumber);
            values[i] = randomNumber;
        }
    }


public boolean searchNumberIterative(int number){
    boolean status = false;
    for (int i=0; i<values.length; i++) {
        if (values[i] == number){
            status = true;
            break;
        }
    }
    return status;
}

Comments

Post a Comment

Let me know your doubts

Popular Posts