انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية تكنولوجيا المعلومات
القسم قسم البرامجيات
المرحلة 2
أستاذ المادة احمد خلفة عبيد العجيلي
11/03/2019 08:34:12
Object-oriented programming with Java Fixed-sized collections Introduction to arrays Ahmed Al-Ajeli Lecture 10 2 Main concepts to be covered •Arrays •Arrays iteration •Multi-dimensional arrays •Miscellaneous topics Object-oriented programming with Java 3 Fixed-size collections - arrays •Sometimes the maximum collection size can be pre-determined. •A special fixed-size collection type is available: an array. •Unlike the flexible-size ArrayList collections, arrays can store object references or primitive-type values. •Arrays are more efficient than ArrayList 4 Creating an array object public class LogAnalyzer { private int[] hourCounts; public LogAnalyzer() { hourCounts = new int[24]; } ... } Array object creation — specifies size Array type — does not contain size Object-oriented programming with Java 5 The hourCounts array 6 Using an array •Square-bracket notation is used to access an array element: hourCounts[...] •Elements are used like ordinary variables. •The target of an assignment: hourCounts[hour] = ...; •In an expression: hourCounts[hour]++; if(hourCounts[hour] > 0) ... Object-oriented programming with Java 7 Standard array use private int[] hourCounts; private String[] names; Private TicketMachine[] machines; ... hourCounts = new int[24]; machines = new TicketMachine[100]; machines[0] = new TicketMachine (500); ... hourcounts[i] = 0; hourcounts[i]++; System.out.println(hourcounts[i]); System.out.println(machines[0].getBalance()); declaration creation use 8 Array literals •Array literals in this form can only be used in declarations. •Related uses require new: private int[] numbers = { 3, 15, 4, 5 }; declaration, creation and initialization numbers = new int[] { 3, 15, 4, 5 }; •The size is inferred from the data. Object-oriented programming with Java 9 Array length •NB: length is a field rather than a method. •It’s value cannot be changed – ‘fixed size’. private int[] numbers = { 3, 15, 4, 5 }; int n = numbers.length; not a method call! 10 Array iteration for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); } int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } for loop version while loop version Object-oriented programming with Java 11 Arrays and the for-each loop int[] hourCounts = {1,3...} ... for(int value : hourCounts) { System.out.println(value); } 12 Practice •Given an array of numbers, print out all the numbers in the array, using a for loop. int[] numbers = { 4, 1, 22, 9, 14, 3, 9}; for ... Object-oriented programming with Java 13 Practice •Fill an array with the Fibonacci sequence. int[] fib = new int[howMany]; fib[0] = 0; fib[1] = 1; for(...) ... 0 1 1 2 3 5 8 13 21 34 ... 14 Arrays of more than one dimension •Array syntax supports multiple dimensions. –E.g., 2D array to represent a game board, or a grid of cells. •Can be thought of as an array of arrays. Object-oriented programming with Java 15 Two-dimensional array example int[][] cells; ... cells = new int[10][20]; ... for(int row = 0; row < 10; row++) { for(int col = 0; col < 20; col++) { cells[row][col] = row + col; } } 16 Alternative iteration •Works with irregular shape arrays, which are supported in Java. for(int row = 0; row < cells.length; row++) { for(int col = 0; col < cells[row].length; col++) { cells[row][col] = row + col; } } Object-oriented programming with Java 17 null •Used with object types. •Used to indicate, no object . •We can test if an object variable holds the null value: if(track == null) … •Used to indicate ‘no track yet’. 18 The conditional operator •Choose between two values inside expressions: int b = (a == 1) ? 20: 30; is equivalent to condition ? value1 : value2 if (a == 1) { b = 20; } else { b = 30; }
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|