Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs212: Data Structures Computer Science Department Lecture 2: Arrays.

Similar presentations


Presentation on theme: "Cs212: Data Structures Computer Science Department Lecture 2: Arrays."— Presentation transcript:

1 Cs212: Data Structures Computer Science Department Lecture 2: Arrays

2 Lecture Contents Arrays What is array? Array declaration
Array initialization Array of objects Multidimensional Arrays Array Advantages and Disadvantages 10-Jan-19 Computer Science Department

3 Arrays Definition An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred to as array elements, can be addressed using a number, called index or subscript. Index/subscript starts begins with 0 Array has fixed size and cannot be changed after it has been declared. 10-Jan-19 Computer Science Department

4 Arrays 10-Jan-19 Computer Science Department

5 Array Name The name of the array is a pointer to its first element
c[ 0 ] -45 6 72 1543 -89 62 -3 1 6453 78 Name of array (note that all elements of this array have the same name, c) c[ 1 ] c[ 2] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] Index (or subscript) of the element in array c c[ 10] 10-Jan-19 Computer Science Department c[ 11 ] Fig. 7.1 A 12-element array.

6 Index Also called subscript Position number in square brackets
Must be positive integer or integer expression a = 5; b = 6; c[ a + b ] += 2; Adds 2 to c[ 11 ] 10-Jan-19 Computer Science Department

7 Examine array c c is the array name c.length accesses array c’s length
c has 12 elements ( c[0], c[1], … c[11] ) The value of c[0] is –45 10-Jan-19 Computer Science Department

8 Array declaration Array must be declared before it is used.
Array occupies a contiguous memory space. The memory spaces an array needs is calculated by multiplying the number of elements by the memory required for their types, example if we have an array of 10 elements, this space is 10*sizeof(float) = 40 bytes 10-Jan-19 Computer Science Department

9 Array declaration con.. One way to declare and initialize an array is as follows: element_type[] array_name = {init_val_0,init_val_1,…,init_val_N−1}; 10-Jan-19 Computer Science Department

10 Declaring and Creating arrays
Arrays are objects that occupy memory Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array We can create arrays of objects too String b[] = new String[ 100 ]; 10-Jan-19 Computer Science Department

11 Create 10 ints for array; each int is initialized to 0 by default
// Fig. 7.2: InitArray.java // Creating an array. import javax.swing.*; 4 public class InitArray { 6 public static void main( String args[] ) { int array[]; // declare reference to an array 10 array = new int[ 10 ]; // create array 12 String output = "Index\tValue\n"; 14 // append each array element's value to String output for ( int counter = 0; counter < array.length; counter++ ) output += counter + "\t" + array[ counter ] + "\n"; 18 JTextArea outputArea = new JTextArea(); outputArea.setText( output ); 21 JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array of int Values", JOptionPane.INFORMATION_MESSAGE ); 25 System.exit( 0 ); 27 } // end main 29 30 } // end class InitArray Create 10 ints for array; each int is initialized to 0 by default Declare array as an array of ints array.length returns length of array array[counter] returns int associated with index in array 10-Jan-19 Computer Science Department

12 Each int is initialized to 0 by default
10-Jan-19 Computer Science Department

13 Example /**Adds all the numbers in an integer array. */ public static int sum(int[] a) { int total = 0; for (int i=0; i < a.length; i++) // note the use of the length variable total += a[i]; return total; } 10-Jan-19 Computer Science Department

14 Arrays are Objects Arrays in Java are special kinds of objects. In fact, this is the reason we can use the new operator to create a new instance of an array. An array can be used just like any general object in Java, but we have a special syntax (using square brackets, "[" and "]") to refer to its members. An array in Java can do everything that a general object can. Since an array is an object, though, the name of an array in Java is actually a reference to the place in memory where the array is stored. 10-Jan-19 Computer Science Department

15 The fact that arrays in Java are objects has an important implication when it comes to using array names in assignment statements. For when we write something like b = a; in a Java program, we really mean that b and a now both refer to the same array. So, if we then write something like b[3] = 5; 10-Jan-19 Computer Science Department

16 10-Jan-19 Computer Science Department

17 Multidimensional Arrays
Can be described as "arrays of arrays". Multidimensional arrays are not limited to two indices. 10-Jan-19 Computer Science Department

18 Memory Representation of 2D Array
10-Jan-19 Computer Science Department

19 Array Advantages and Disadvantages
Easier to declare and use. Can be used with most of the data types. Array Disadvantages: Fixed size data. If not all the array elements are used. waste of memory space. If more array elements are required if the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values. 10-Jan-19 Computer Science Department


Download ppt "Cs212: Data Structures Computer Science Department Lecture 2: Arrays."

Similar presentations


Ads by Google