200 likes | 548 Views
Cs212: Data Structures. Lecture 2: Arrays. Lecture Contents. Arrays What is array? Array declaration Array initialization Array of objects Multidimensional Arrays Array Advantages and Disadvantages. Arrays Definition.
E N D
Cs212: Data Structures Computer Science Department Lecture 2: Arrays
Lecture Contents • Arrays • What is array? • Array declaration • Array initialization • Array of objects • Multidimensional Arrays • Array Advantages and Disadvantages Computer Science Department
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. Computer Science Department
Arrays Computer Science Department
-45 6 0 72 1543 -89 0 62 -3 1 6453 78 Array Name • The name of the array is a pointer to its first element c[ 0 ] 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 ] c[ 10] Index (or subscript) of the element in array c Computer Science Department c[ 11 ] Fig. 7.1 A 12-element array.
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 ] Computer Science Department
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 Computer Science Department
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 Computer Science Department
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}; Computer Science Department
Declaring and Creating arrays • Arrays are objects that occupy memory • Created dynamically with keyword new int c[] = newint[ 12 ]; • Equivalent toint c[]; // declare array variable c = newint[ 12 ]; // create array • We can create arrays of objects too String b[] = new String[ 100 ]; Computer Science Department
Declare array as an array of ints Create 10ints for array; each int is initialized to 0 by default array.length returns length of array array[counter] returns int associated with index in array • 1 // Fig. 7.2: InitArray.java • 2 // Creating an array. • 3 importjavax.swing.*; • 4 • 5 public classInitArray { • 6 • 7 public static void main( String args[] ) • 8 { • 9 int array[]; // declare reference to an array • 10 • 11 array = new int[ 10 ]; // create array • 12 • 13 String output = "Index\tValue\n"; • 14 • 15 // append each array element's value to String output • 16 for ( int counter = 0; counter < array.length; counter++ ) • 17 output += counter + "\t" + array[ counter ] + "\n"; • 18 • 19 JTextAreaoutputArea = newJTextArea(); • 20 outputArea.setText( output ); • 21 • 22 JOptionPane.showMessageDialog( null, outputArea, • 23 "Initializing an Array of int Values", • 24 JOptionPane.INFORMATION_MESSAGE ); • 25 • 26 System.exit( 0 ); • 27 • 28 } // end main • 29 • 30 } // end class InitArray Computer Science Department
Each int is initialized to 0 by default Computer Science Department
Example /**Adds all the numbers in an integer array. */ public static intsum(int[] a) { inttotal = 0; for (inti=0; i < a.length; i++) // note the use of the length variable total += a[i]; return total; } Computer Science Department
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. Computer Science Department
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; Computer Science Department
Multidimensional Arrays • Can be described as "arrays of arrays". • Multidimensional arrays are not limited to two indices. Computer Science Department
Memory Representation of 2D Array Computer Science Department
Array Advantages and Disadvantages Array Advantages: • 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. Computer Science Department