Bài giảng An Introduction to Computer Science Using Java - Chapter 8 One-Dimensional Arrays

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 8 One-Dimensional Arrays: Chapter 8 One-Dimensional ArraysLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce the array as a structure for storing large amounts of datadiscuss common array operationsintroduce algorithms for searching and sorting arraysshow how multiple images can be painted from an array to use in programming simple animationsArray DeclarationsArrays contain a fixed number of variables of identical typeArray declaration and allocation are separate operationsDeclaration examples:int[] counts;double[] scores;String[] studentNames; Array AllocationArrays are allocated using the Java new operatorThe syntax is: new type[size];Examples:counts = new int[10];scores = new double[15];studentNames = new String[10];Array Organization 0 1 2 3 4 5 6 7 8 9 countsEach box is an int variableThe numbers on top are each variable’s subscript or indexAn array of size 10 has subscripts 0 to 9Arra...

ppt29 trang | Chia sẻ: honghanh66 | Lượt xem: 992 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 8 One-Dimensional Arrays, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 8 One-Dimensional ArraysLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce the array as a structure for storing large amounts of datadiscuss common array operationsintroduce algorithms for searching and sorting arraysshow how multiple images can be painted from an array to use in programming simple animationsArray DeclarationsArrays contain a fixed number of variables of identical typeArray declaration and allocation are separate operationsDeclaration examples:int[] counts;double[] scores;String[] studentNames; Array AllocationArrays are allocated using the Java new operatorThe syntax is: new type[size];Examples:counts = new int[10];scores = new double[15];studentNames = new String[10];Array Organization 0 1 2 3 4 5 6 7 8 9 countsEach box is an int variableThe numbers on top are each variable’s subscript or indexAn array of size 10 has subscripts 0 to 9Array SubscriptsArrays can contain any one type of value (either primitive values or references)Subscripts are used to access specific array valuesExamples:counts[0] // first variable in countscounts[1] // second variable in countscounts[9] // last variable in countscounts[10] // error – trying to access // variable outside counts Expressions as SubscriptsArray subscripts do not have to be constantsArray subscripts do need to be integer expressions that evaluate to valid subscript values for the current array allocationExamples:counts[i]counts[2*i]counts[I/2]Array InitializationArrays can be initialized by giving a list of their elementsIf your list contains n elements the subscripts will range from 0 to n – 1You do not need to allocate the array explicitly after it is initializedExample:int [] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; Initializing an Array of Stringsfinal Sring[ ] NAME = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};// procedure that prints the day of weekpublic void printName (int day, OutputBox out) { out.print(NAME[day – 1]);} AliasesIt is possible to have two different variables refer to the same arrayWhen this happens these variables are called aliasesCreating aliases is not a good programming practiceExample of how it can happen:int [ ] A, B;B = new int [10];A = B;Loops and Array ProcessingInitializes counts to 0, 10, 20, , 90for (int i=0; i = 0 && A[j] > Ai) { A[j + 1] = A[j]; j--; } A[j + 1] = A[i]; } }} Linear Searchint linearSearch (int[] A, int key)int i;for (i = 0; i < A.length; i++) { // key not in A[0]..A[i - 1] if (A[i] == key) return i; // key not in A return –1;} SearchingWhen searching for int values you can test for exact matches using (A[i] == key)Comparing two double values for equality will not always give a correct resultA better comparison for doubles would be (Math.abs(A[i] – key) < epsilon)Epsilon should be as small a value as is acceptable for your application Using Arrays in AnimationImage[] mouse = new Image[NUMBER];Int[] sleepTime = {1540, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240};public void show( ) { for (int i = 0; i < NUMBER; i++) mouse[i] = Toolkit.getDeafaultTool(). getImage(“images/T”+(i+1)+”gif); ticker = 0; do { g.drawImage(mouse[ticker}, 70, 70); Timer.pause(sleepTime{ticker]); ticker = (ticker + 1) % NUMBER; } while (true); }}

Các file đính kèm theo tài liệu này:

  • pptchapter8_7925.ppt