第7章数组和向量 G理解数组的概念 学习目 学习使用数组 a熟悉排序和查找算法 a学会如何使用多维数组 熟悉数字包装类和他们的子类 G了解如何使用命令行参数 际-使用 Vector类
第7章 数组和向量 )理解数组的概念 )学习使用数组 )熟悉排序和查找算法 )学会如何使用多维数组 )熟悉数字包装类和他们的子类 )了解如何使用命令行参数 )使用Vector类
Introducing Arrays 数组是相同类型的数据按顺序组成mN0 的一种复合数据类型,通过数组名吗以叫 和下标,可以使用数组中的数据。 myList[3 下标从0开始。数组是所有编程语m4 言中常用的数据结构。 myList[5] myList[6] myList[7 myList[8 doub1e类型的10个数组元素 myList[9] doublel my list= new double 10
Introducing Arrays myLis t[0] myLis t[1] myLis t[2] myLis t[3] myLis t[4] myLis t[5] myLis t[6] myLis t[7] myLis t[8] myLis t[9] 数组是相同类型的数据按顺序组成 数组是相同类型的数据按顺序组成 的一种复合数据类型,通过数组名 的一种复合数据类型,通过数组名 和下标,可以使用数组中的数据。 和下标,可以使用数组中的数据。 下标从 0开始。数组是所有编程语 开始。数组是所有编程语 言中常用的数据结构。 言中常用的数据结构。 double 类型的10个数组元素 : double[] myList = new double[10]
Declaring Arrays datatype[] arraynamei Example: int[] mylisti datatype arrayname[] Example: int myList[li
Declaring Arrays ) datatype[] arrayname; Example: int[] myList; ) datatype arrayname[]; Example: int myList[];
Creating arrays arrayName new datatype[arraysizeli Example: myList new double[10]
Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10];
Declaring and Creating in One step datatype [] arrayname =new datatype [arraysize]i double [ myList new double [10]i datatype arrayname[] new datatype [arraysizeli double myList[] new double [10]
Declaring and Creating in One Step ) datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10]; ) datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10];
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 测试二分查找法
对象的数组 Declaring and creating. Circle[] circleArray new Circle[101 G Initializing: for (int i=0; i<circleArray length; i++) circleArray [i] new Circled)i
对象的数组 ) Declaring and creating: Circle[] circleArray = new Circle[10]; ) Initializing: for (int i=0; i<circleArray.length; i++) { circleArray[i] = new Circle(); }