cs212: data structures
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
CS212: DATA STRUCTURES
Lecture 2: ArraysComputer Science Department
1
19 Apr 2023 Computer Science Department
2
Lecture Contents
Arrays What is array? Array declaration Array initialization Array of objects Multidimensional Arrays Array Advantages and Disadvantages
19 Apr 2023 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.
19 Apr 2023 Computer Science Department
4
Arrays
19 Apr 2023
5
Array Name
Computer Science Department
The name of the array is a pointer to its first elementName of array
(note that all elements of this array have the same name, c)
Index (or subscript) of the element in array c
c[ 0 ]
c[ 1 ]
c[ 2]
c[ 3 ]
c[ 4 ]
c[ 5 ]
c[ 6 ]
c[ 7 ]
c[ 8 ]
c[ 9 ]
c[ 10]
c[ 11 ]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
19 Apr 2023 Computer Science Department
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 ]
19 Apr 2023 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
Computer Science Department
8
Array declaration
19 Apr 2023
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
19 Apr 2023 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};
19 Apr 2023 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 tooString b[] = new String[ 100 ];
19 Apr 2023 Computer Science Department
11
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
Declare array as an array of ints
Create 10 ints 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
19 Apr 2023 Computer Science Department
12
Each int is initialized to 0 by default
19 Apr 2023 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; }
19 Apr 2023 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.
19 Apr 2023 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;
19 Apr 2023 Computer Science Department
16
19 Apr 2023 Computer Science Department
17
Multidimensional Arrays
Can be described as "arrays of arrays". Multidimensional arrays are not limited to two
indices.
19 Apr 2023 Computer Science Department
18
Memory Representation of 2D Array
19 Apr 2023 Computer Science Department
19
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.