From 9583d82a23367d344d529378204e92925525cf51 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 10 Aug 2024 10:45:42 +0530 Subject: [PATCH] Refactored Code for For:Each, not For. --- OOP/Java/Lab/Week3/ArraySearch.java | 50 ++++++++++++++++------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/OOP/Java/Lab/Week3/ArraySearch.java b/OOP/Java/Lab/Week3/ArraySearch.java index 9b57821..b31da60 100644 --- a/OOP/Java/Lab/Week3/ArraySearch.java +++ b/OOP/Java/Lab/Week3/ArraySearch.java @@ -1,23 +1,27 @@ -import java.util.Scanner; -public class ArraySearch { - public static void main(String args[]){ - Scanner sc = new Scanner(System.in); - int flag=0; - - int[] array = {1,2,3,1,5,6,1,8,9}; - - System.out.println("Enter the search value:"); - int b = sc.nextInt(); - - System.out.println("The value is found at the locations: "); - for(int i=0;i<9;i++){ - if(array[i] == b){ - System.out.print(" a["+i+"]"); - flag++; - } - } - if(flag==0){ - System.out.print("None. The input value does not exist in the array."); - } - } -} +import java.util.Scanner; +public class ArraySearch { + public static void main(String args[]){ + Scanner sc = new Scanner(System.in); + int flag=0,index=0; + + int[] array = {1,2,3,1,5,6,1,8,9}; + + System.out.println("Enter the search value:"); + int b = sc.nextInt(); + + System.out.println("The value is found at the locations: "); + + + for(int i:array){ + if(i == b){ + System.out.print(" a["+index+"]"); + flag++; + } + index++; + } + + if(flag==0){ + System.out.print("None. The input value does not exist in the array."); + } + } +}