Arrays

advertisement
Arrays
Copy of a Variable
• Two variables are independent of each other if
their values can change independently of each other
Copying an Array (1)
Copying an Array (2)
• The array element identified by the name a[0] is
the same array element that is identified by the name b[0]
• The statement b = a; did not make a (true) copy of an array
• Aliasing = using different names to access the same variable
Copying an Array (3)
Copying an Array (4)
• In Java, we can change an existing array reference to make
it point to a different set of array elements
• The Java programming system has a built-in utility that reclaim
the memory cells used by inaccessible variables (= garbage)
Copying an Array (5)
In order to copy an array in Java, we must:
• Create another array (of the same size) using
the new operator
• Copy the elements (one by one) from the old array into
the new array.
Copying an Array (6)
Static and dynamic arrays (1)
• Static array = an array name that refers to an array where
the number of elements in the array is constant (fixed)
• Dynamic array = an array name that refers to an array where
the number of elements in the array can be changed
Static and dynamic arrays (2)
• We first create a new array of the desired length
• Then, we must copy the elements from the old array into
the newly created array
• Finally, we make the variable a point to the new array.
Static and dynamic arrays (3)
Static and dynamic arrays (4)
String Array and Command Line Argument
Command arguments (1)
• Command = an application program that is being executed
• Argument = the words that follow the command
Command arguments (2)
Command arguments (3)
Command arguments (4)
Converting String to array of
char and vice versa (1)
Converting String to array of
char and vice versa (2)
Converting String to array of
char and vice versa (3)
Example
Download