Home / Programs / Write a Java program to search multiple values in an array.
🚀 Programming Example

Write a Java program to search multiple values in an array.

👁 21 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

Given Input:


 int[] arr = {5, 10, 15, 20, 25};
        int[] searchValues = {10, 25, 30};

Expected Output:

10 found at index 1
25 found at index 4
30 not found

💻 Program Code

class SearchMultipleElements {
    public static void main(String[] args) {
        int[] arr = {5, 10, 15, 20, 25};
        int[] searchValues = {10, 25, 30};

        for (int i = 0; i < searchValues.length; i++) {
            boolean found = false;

            for (int j = 0; j < arr.length; j++) {
                if (arr[j] == searchValues[i]) {
                    System.out.println(searchValues[i] + " found at index " + j);
                    found = true;
                    break;
                }
            }

            if (!found) {
                System.out.println(searchValues[i] + " not found");
            }
        }
    }
}

                        

📘 Explanation

The program uses nested loops to search multiple values one by one inside the same array.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.