mit java open course assignment #2
package come; public class Marothon { public static void FirstName(String[] args1,int[] args2){ int MinIndex,MinTime,i; MinTime = 10000; MinIndex = 0; for(i = 0; i < 16; i++){ if (args2[i] < MinTime){ MinTime = args2[i]; MinIndex = i; } } System.out.println(args1[MinIndex] + " "+MinTime); } public static void SecondName(String[] args1, int[] args2){ int SmallIndex,SmallerIndex,Small,Smaller,i; Small = 10000; Smaller = 10000; SmallerIndex = 100; SmallIndex =101; for(i = 0; i<16; i++){ if(args2[i] < Small && args2[i] > Smaller){ Small = args2[i]; SmallIndex = i; }else if(args2[i] < Smaller){ Small = Smaller; Smaller = args2[i]; SmallIndex = SmallerIndex; SmallerIndex = i; } } System.out.println(args1[SmallIndex] + " " + args2[SmallIndex]); } public static void main(String[] args){ // TODO Auto-generated method stub String[] names = {"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"}; int[] times = {341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265}; SecondName(names,times); } }
SecondName函数只遍历了一次数组,大大提升了之前想用FirstName遍历一次后用if避开最小的来做,不过就要遍历两次,哈哈哈,不错