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 10-Jan-19 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. 10-Jan-19 Computer Science Department
Arrays 10-Jan-19 Computer Science Department
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.
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
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
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
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
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
Create 10 ints for array; each int is initialized to 0 by default 1 // Fig. 7.2: InitArray.java 2 // Creating an array. 3 import javax.swing.*; 4 5 public class InitArray { 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 JTextArea outputArea = new JTextArea(); 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 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
Each int is initialized to 0 by default 10-Jan-19 Computer Science Department
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
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
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
10-Jan-19 Computer Science Department
Multidimensional Arrays Can be described as "arrays of arrays". Multidimensional arrays are not limited to two indices. 10-Jan-19 Computer Science Department
Memory Representation of 2D Array 10-Jan-19 Computer Science Department
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