package ArraysEx; // examples of searching through an array and storing the location public class SearchArray { public static void main(String[] args) { // parallel arrays of names and grades int[] grades = new int[]{ 73, 91, 55, 84, 69, 82, 84, 99, 92, 71}; String[] names = new String[]{"Arturo", "Bozo", "Constance", "Vex", "Vax", "Vix", "Sherlock", "Havelock", "Moloch", "Jake"}; //find highest grade -- not index ----------------------------------------- // highest grade seen int high = grades[0]; // start at first grade -- we know some grade is highest, this is first possibility // look through all values in array // since we already looked at 0th grade, start at 1 (would still work starting at 0 for (int i = 1; i < grades.length; i++){ // if current grade is higher than highest so far if (grades[i] > high){ // record new high high = grades[i]; } } System.out.println("Highest grade was " + high + " but we don't know who that was "); //find INDEX of student with highest grade -- version with two variables ----------------------------------------- // highest grade seen int highGrade = grades[0]; // start at first grade -- we know some grade is highest, this is first possibility // index where we find the highest grade int highIndex = 0; // start at first index -- we know some grade is highest, this is first possibility // look through all values in array // since we already looked at 0th grade, start at 1 (would still work starting at 0 for (int i = 1; i < grades.length; i++){ // if current grade is higher than highest so far if (grades[i] > highGrade){ // record new high highGrade = grades[i]; // remember WHERE we saw that new high -- at position i in the array highIndex = i; } } System.out.println("Highest grade was " + grades[highIndex] + " by " + names[highIndex]); //find INDEX of student with highest grade -- version with one variable ----------------------------------------- // index where we find the highest grade int highGradeIndex = 0; // start at first index -- we know some grade is highest, this is first possibility // look through all values in array // since we already looked at 0th grade, start at 1 (would still work starting at 0 for (int i = 1; i < grades.length; i++){ // if current grade is higher than highest so far // (based on location where we saw last high if (grades[i] > grades[highGradeIndex]){ // remember WHERE we saw that new high -- at position i in the array highGradeIndex = i; } } System.out.println("Highest grade was " + grades[highGradeIndex] + " by " + names[highGradeIndex]); // search array to see if a value is there at all --------------------------------------------------- // value we are searching for int searchFor = 69; // chosen arbitrarily (sometimes we're 12) // index where we find it, if we find it int foundIndex = -1; // start at impossible index -- it might not be there at all // look through each element of array for (int i = 0; i < grades.length; i++) { // if the current element is the one we're looking for if (grades[i] == searchFor) { // remember WHERE we found that value -- at position i in the array foundIndex = i; } } // if we got all the way through the loop and this is still -1 // we never saw the value, so it isn't in the array if (foundIndex == -1) { System.out.println("Nobody had the funny score."); // if it got changed, we must have found it } else { System.out.println("At least " + names[foundIndex] + "'s score is funny"); } } }