Arrays
Arrays
Prepared by:
2 4 6
1 3 5 7
1 2 3 4 5 6 7
Introducing Arrays
Array is a data structure that represents a collection
of the same types of data.
double[] myList = new double[10];
• datatype arrayname[];
Example:
double myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new double[10];
arrayVariabIe.iength
For exampie,
myList.length returns
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] =i;
TestArray Run
Assigning Grades
TestPassArra
Example
swap(a[0], a[1]) a[0] 1 a[1] 2
Pass by value
TestCopyArray Run
Copying Arrays
Before the assignment After the assignment
list2 = list1; list2 = list1;
list1 listl
Contents Contents
of listl of listl
list2 list2
Contents Contents
of list2 of list2
Garbage
Copying Arrays
Using aloop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
double[][] x;
ultidimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = (
{1, 2, 31,
matrix = new int[5][5]: matrix[2] [1] = 7; {4, 5, 6},
t 7, 8, 9 j,
10, 11, 12
Declaring, Creating, and Initializing Using Shorthand
Notations
You can also use a shorthand notation to declare, create and initialize a two-dimensional
array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
array.length
array[0].length
array[1].length
array[2].length
Definition Pointer
O 1 > 3 4 5 6 7 8 9 10 11
heap
address space (dynamicall
code
PC
OXOOOOOOOO
(text)
Declaring Pointers in
• int *p; a pointer to an int
• double *q; a pointer to a double
• char **r; a pointer to a pointer to a char
i1= 1; 0x100C
i2 = 2;
0x1008
*ptrl = 3;
i2 =*ptr2;
Pointer Arithmetic
• int *p, *q;
q = p +1;
° Construct a pointer to the next integer after *p and
assign it to q
• double *p, *r;
int n;
r = p + n;
• Construct a pointer to a double that is n doubles beyond
*p, and assign it to r
• n may be negative
Pointer Arithmetic (continued)
• float *p, *q;
int n;
n = p - q;
° n is the number of floats between *p and *q; i.e., what
would be added to q to get p
Pointer Arithmetic
pointer + number pointer —number
*x= 1001; a
*y = 1002 ;
b
int b 2;
set x and y(&a, b);
Arrays and Pointers
• Arrays and pointers are closely related in C
• In fact, they are essentially the same thing!
• Esp. when used as parameters of functions
• int A[10];
int *p;
• Type of A is int *
• p = A; and A = p; are legal assignments
° *p refers toA[O]
*(p + n) refers to A[n]
• p = &A[5]; is the same as p = A + 5;
Arrays and Pointers
• double A[10]; vs. double *A;
• Only difference:—
° double A[10] sets aside ten units of memory, each large
enough to hold a double
° double *A sets aside one pointer-sized unit of memory
• You are expected to come up with the memory elsewhere!
° Note:— all pointer variables arethe same size in any given
machine architecture
• Regardless of what types they point to
ote
• C does not assign arrays to each other
• E.g,
• double A[10];
double B[10];
A = B;
• assigns the pointer value B to the pointer value A
• Contents of array A areuntouched
Arrays as Function Parameters
• void init(float A[], int arraysize);
void init(float *A, int arraysize);