Initializing arrays Using a loop for (int i=0; i< myList. length; i++) myList[i]=(double)i G Declaring, creating initializing in one step doub1e[] myList={1.9,2.9,3.4,3.5};
Initializing Arrays ) Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = (double)i; ) Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5};
Example 7.1 assigning grades Objective: read student scores(int) from the keyboard, get the best score, and then assign grades based on the following scheme Grade is a if score is > best-10 Assign Grade Grade is b if score is > best-20 Run Grade is c if score is >s best-30 Grade is d if score is > best- 40 Grade is f otherwise
Example 7.1 Assigning Grades ) Objective: read student scores (int) from the keyboard, get the best score, and then assign grades based on the following scheme: – Grade is A if score is >= best–10; – Grade is B if score is >= best–20; – Grade is C if score is >= best–30; – Grade is D if score is >= best–40; – Grade is F otherwise. AssignGra d e Run
Example 7.2 Using arrays in Sorting Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers Selection Sort Run
Example 7.2 Using Arrays in Sorting ) Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. SelectionSort Run
Example 7.3 Testing linear search Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search Linearsearch Run 例74测试二分查找法
Example 7.3 Testing Linear Search ) Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search. LinearSearch Run 例7.4 测试二分查找法