/
ARRAYS  CT 1513 One-Dimensional ARRAYS  CT 1513 One-Dimensional

ARRAYS CT 1513 One-Dimensional - PowerPoint Presentation

oryan
oryan . @oryan
Follow
66 views
Uploaded On 2023-09-25

ARRAYS CT 1513 One-Dimensional - PPT Presentation

1 Introduction 2 Properties of an Array 3 Arrays Declaration 31 Examples 32 Using constants as sizes 33 Using expressions as sizes 34 Specifying sizes during execution 35 Initialization during declaration ID: 1021251

array list programming program list array program programming arrays problem design analysis elements java int length variable number declaration

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "ARRAYS CT 1513 One-Dimensional" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. ARRAYS CT 1513One-Dimensional

2. 1. Introduction2. Properties of an Array3. Arrays Declaration 3.1 Examples 3.2 Using constants as sizes 3.3 Using expressions as sizes 3.4 Specifying sizes during execution 3.5 Initialization during declaration 4. Accessing Array Elements5. length6. Initialization of the array 6.1 Initialization during declaration 6.2 Using loops 6.3 User input7. Programming Hint (1)8. Programming Hint (2)Outline2

3. 1. INTRODUCTION3Simple data types (int, double, char, boolean) use a single memory cell to store a variable. For example, after the declaration of the following three variables (sum, choice and average), the memory layout may look as shown in the figure:int sum = 0;char choice = ‘x’;double average = 0.0;123Variables are dispersed in memoryNames are translated into addressesThe order of declaration is not necessarilythe order of storing in the memory. In fact,we don’t have any control on assigning addressesto variables.Address Value200x50005020.0

4. 2. PROPERTIES OF AN ARRAY4An array is a collection of two or more adjacent memory cells called array elements.Elements of the same array should be all of the same type: this represents the type of the array.Array elements are associated with a particular symbolic name (identifier name) that obeys all rules previously mentioned about identifiers.Therefore, to set up an array in memory, we must declare the following:The type of the array: which is the type of its elementsThe name of the arrayThe number of elements of the array: this is the number of adjacent memory cells associated with the array nameHowever, it is sometimes more efficient to solve problems by grouping data items together for storage in main memory.For such types of problems, we use an array.Grouped data items are then processed using a loop.

5. One-Dimensional ArraysJava Programming: From Problem Analysis to Program Design, 4e5

6. One-Dimensional Arrays (continued)Java Programming: From Problem Analysis to Program Design, 4e6

7. Array num:int[] num = new int[5];7Java Programming: From Problem Analysis to Program Design, 4eArrays initializationWhen an array is instantiated, Java automatically initializes its elements to their default values.numeric arrays are initialized to 0,char arrays are initialized to the null character, which is '\u0000',boolean arrays are initialized to false.

8. Array num:int[] num = new int[5];Java Programming: From Problem Analysis to Program Design, 4e8Arrays (continued)To save space, we also draw an array, as shown in Figures 9-2(a) and 9-2(b).

9. One-Dimensional Arrays : access elementsJava Programming: From Problem Analysis to Program Design, 4e9intExp = number of components in array >= 00 <= indexExp < intExp

10. ACCESSING ARRAY ELEMENTSJava Programming: From Problem Analysis to Program Design, 4e10

11. ACCESSING ARRAY ELEMENTSJava Programming: From Problem Analysis to Program Design, 4e11

12. ACCESSING ARRAY ELEMENTSJava Programming: From Problem Analysis to Program Design, 4e12

13. ACCESSING ARRAY ELEMENTS13Example 2Consider the following declarations and their reflection in the memory:double[] list = new double[10];1list[0]list[1]list[2]list[3]list[4]list[5]list[6]list[7]list[8]list[9]list0.00.00.00.00.00.00.00.00.00.0int i = 2;list[2*i-1] = 46.0; //list[3] = 46.023list[0]list[1]list[2]list[3]list[4]list[5]list[6]list[7]list[8]list[9]list0.00.00.046.00.00.00.00.00.00.0list[2*i+1] = 20.0; // i = 24list[0]list[1]list[2]list[3]list[4]list[5]list[6]list[7]list[8]list[9]list0.00.00.046.00.020.00.00.00.00.0list[7] = list[3] + list[5]; //list[7] = 46.0 + 20.0 = 66.05list[0]list[1]list[2]list[3]list[4]list[5]list[6]list[7]list[8]list[9]list0.00.00.046.00.020.00.066.00.00.0

14. ACCESSING ARRAY ELEMENTS14 Note the difference between the following two statements:list[7] = list[3] + list[5]; //list[7] = 46.0 + 20.0 = 66.0list[3+5] = 10.0; //list[8] = 10.056list[0]list[1]list[2]list[3]list[4]list[5]list[6]list[7]list[8]list[9]list0.00.00.046.00.020.00.066.010.00.0

15. Use constant to declare array size15Java Programming: From Problem Analysis to Program Design, 4e

16. Java Programming: From Problem Analysis to Program Design, 4e16Specifying Array Size During Program Execution

17. Java Programming: From Problem Analysis to Program Design, 4e17The initializer list contains values, called initial values, that are placed between braces and separated by commas sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90, sales[3]= 23.00, and sales[4]= 45.68Array Initialization During Declaration

18. Array Initialization During Declaration (continued)Java Programming: From Problem Analysis to Program Design, 4e18int[] list = {10, 20, 30, 40, 50, 60};When declaring and initializing arrays, the size of the array is determined by the number of initial values within the braces.If an array is declared and initialized simultaneously, we don’t use the operator new to instantiate the array object

19. Arrays and the Instance Variable lengthJava Programming: From Problem Analysis to Program Design, 4e19Associated with each array that has been instantiated, there is a public (final) instance variable lengthThe variable length contains the size of the array The variable length can be directly accessed in a program using the array name and the dot operator

20. Arrays and the Instance Variable length (continued)Java Programming: From Problem Analysis to Program Design, 4e20int[] list = {10, 20, 30, 40, 50, 60};This statement creates the array list of six components and initializes the components using the values givenHere list.length is 6int[] numList = new int[10]; This statement creates the array numList of 10 components and initializes each component to 0

21. Arrays and the Instance Variable length (continued)Java Programming: From Problem Analysis to Program Design, 4e21The value of numList.length is 10 numList[0] = 5; numList[1] = 10; numList[2] = 15; numList[3] = 20;These statements store 5, 10, 15, and 20, respectively, in the first four components of numList You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElementIt is a common practice for a program to keep track of the number of filled elements in an array

22. Array Index Out of Bounds Exception double[] num = double[10]; int i; The element num[i] is valid, that is, i is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. The index—say, index—of an array is in bounds if index >= 0 and index <= arraySize - 1. If either index < 0 or index > arraySize - 1, then we say that the index is out of bounds. Java Programming: From Problem Analysis to Program Design, 4e22

23. Note on Array DeclarationQ: What is the difference between the following declaration ?int alpha[], beta; int[ ] gamma, delta; Java Programming: From Problem Analysis to Program Design, 4e23

24. PROGRAMMING HINT24The size of the array specified during instantiation cannot be changed.Sometimes, the programmer does not know how much elements will be needed.Therefore, it is common to specify a large size and use only the elements which were actually filled by the user.Assume that numOfElements is a variable that represents the number of actually filled elements within the array (numOfElements <= length)The value of numOfElements is used as a counter: it is initialized to zero, and then incremented with each new element filled in the array.

25. 25Self-Check Exercises Write a complete Java program that does the following:Initializes all array elements with the value ‘*’Accepts a set of Strings, takes the first character of the String and fills it in the array in sequence.Prints the array.W8.1 Arrays (1)Write a complete Java program that stores the user input of integers into an array, and then initializes another array of the same size. The program then adds the corresponding elements of both arrays and puts the result in a third one.Write a complete Java program that stores the user input of integers into an array, and then prints it in the reverse order.

26.

27. Arrays of Objects27

28. Arrays of Objects28Can use arrays to manipulate objectsExample: create array named array1 with N objects of type T T[] array1 = new T[N] Can instantiate array1 as follows: for (int j = 0; j <array1.length; j++) array1[j] = new T();

29. Array of String Objects29String[] nameList = new String[5]; nameList[0] = "Amanda Green"; nameList[1] = "Vijay Arora"; nameList[2] = "Sheila Mann"; nameList[3] = "Rohit Sharma"; nameList[4] = "Mandy Johnson";

30. Array of String Objects(continued)30

31. Arrays of Objects (continued)31Clock[] arrivalTimeEmp = new Clock[100];

32. Instantiating Array Objects32for (int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();

33. Instantiating Array Objects (continued)33arrivalTimeEmp[49].setTime(8, 5, 10);