## Lecture 2: Arrays, Objects, Classes, and Strings - Part 1

Part 1: ARRAYS

An array is a list of variables of thesametype. The list is indexed, and theindex. For example, if A is an array of 10 integers, then your array is indexed asalwaysstarts at zero

- A
_{0}, A_{1}, A_{2}, ..., A_{9}

- The elements of an array are accessed using square brackets. All other rules for assignments apply as usual.
Example:

- If A is an array of 5 numbers, we can store a number in the third element of A by saying
A[2] = 102;

- Note that the third element has the index 2 since the array starts at 0.
- To add up the first and last element of A and store the result in a new integer variable, we say:

int sum = A[0] + A[4];- Like any other type of variables, and array has to be declared before it can be used. Declaring an array in Java is usually a three step process:

defining the array allocating memory for the array populating the array - In general, whenever memory is allocated, you find the Java keyword
newthat will accomplish that (there are some exceptions). Sometimes all three steps can be accomplished simultaneously. Here are the three possibilities:Declaring an Array:Method 1:Declaring an array, allocating memory, and populating the array in three seperate steps. Note that the three steps do not have to be successive lines. You can declare an array at the beginning of a program segment without specifiying its actual size. Later, perhaps after user input, you can allocate memory for an array of a specific size, and later yet you can populate the array.type[] varName;varName = new int[size];for (int i = 0; i < size; i++)A[i] = expression;;

**Method 2:**Declaring an array and allocating memory in one step, then populating the array. Again, the two steps do not have to be on successive lines.`type[] varName = new type[size];``for (int i = 0; i < size; i++)``varName[i] = expression;`**Method 3:**The implicit method lets you do all three steps in one. The compiler will automatically determine the size of the array, allocate memory, and assign values.`type[] varName = {value1, value2, ..., valueN};`**Examples:**- Let's use the first method to define an array of integers (of so far undetermined size), specify it to be of length 10, and then store the square of its index in the array:
**Two or higher dimensional arrays**are handled in the same way. Recall that in mathematics a two dimensional array is often called a matrix, while you can also think of it as a table with a certain number of rows and columns. Here are a few examples:- Here are a few, very simple examples on using one- and two-dimensional arrays in complete programs.
**Example 1:**Define an array of 100 integers, populate it with the numbers 1 to 100. Then find the sum of the array and the sum of the square of the array.**Example 2:**Defiine a 4 by 4 square matrix, or a table with 4 rows and 4 columns, and initialize it with numbers 1 to 16. Find the sum of the major and the minor diagonals. Recall that the major diagonal are those numbers in the diagonal of a table from upper left to the lower right. The minor diagonal are those numbers in the diagonal of a table from upper right to lower left.**Assignment:**Write a program that computes the average of the first 20square roots, and prints out the difference between each square root and that average.**Hints:**- Now that we know about the bare essentials of the Java programming language it is time to explore the most prominent feature of the language: objects, classes, and methods. That's the next part of the lecture...

int[] A; // declares A to be an array of integers A = new int[10] // allocates memory to hold 10 integers (A[0], …, A[9]) for (int i = 0; i < 10; i++) A[i] = i*i; // populates the array with integers

Here's another example. Define an array of 10 integers, then store the square of its index in the array.

int[] B = new int[10] // declares Aandallocates memory for (int i = 0; i < 10; i++) B[i] = i * i; // populates the array with integers

Finally, define an array to contain the positive even number up to 20.

int[] C = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} // declares array A, &nb sp; // allocates memory and &nb sp; // populates the array

int[][] D; // D is a two dimensional array D = new int[3][4] // allocates memory for 3 rows, 4 columns for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) D[i][j] = i * j; // populates the matrix

This would result in a table with 3 rows, 4 columns, or a 3 x 4 matrix, as follows:

D[0][0] = 0 |
D[0][1] = 0 |
D[0][2] = 0 |
D[0][3] = 0 |

D[1][0] = 0 |
D[1][1] = 1 |
D[1][2] = 2 |
D[1][3] = 3 |

D[2][0] = 0 |
D[2][1] = 2 |
D[2][2] = 4 |
D[2][3] = 6 |

In the above example, we have used a loop inside a loop to populate the array after it has been declared and memory was allocated. As before, these three steps of declaring, allocating, and populating can be combined:

int[][] E = new int[3][4] // declares E as 3 by 4 matrixandallocates memory for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) E[i][j] = i * j; // populates the matrix

or

int[][] E = { {1,2,3}, {4,5,6}, {7, 8, 9}}; // declares, allocates, and populates

public class Test { public static void main(String[] args) { int A[] = new int[100]; // initializing the array for (int i = 0; i < 100; i++) A[i] = i+1;// why the i + 1 as opposed to just i ?int sum = 0; for (int i = 0; i < 100; i++) sum += A[i]; // shortcut for sum = sum + A[i] int sqsum = 0; for (int i = 0; i < 100; i++) sqsum += A[i]*A[i]; // shortcut for sqsum = sqsum + A[i]*A[i] System.out.println("Sum of numbers 1 to 100 is " + sum); System.out.println("Sum of first 100 square numbers is " + sqsum); } }

public class Test { public static void main(String[] args) { int M[][] = new int[4][4]; // declaring and allocating for (int row = 0; row < 4; row++) // populating array for (int col = 0; col < 4; col++) M[row][col] = row * 4 + col + 1;// are these really 1 through 16 ? // can you do declaring, allocating, and populating all in one step? How ?int majorsum = 0; for (int diag = 0; diag < 4; diag++) majorsum += M[diag][diag]; int minorsum = 0; for (int diag = 0; diag < 4; diag++) minorsum += M[diag][3-diag]; System.out.println("Sum of major diagonal is " + majorsum); System.out.println("Sum of minor diagonal is " + minorsum); } }

Your program, as always, start out like: |

public class Test4 { public static void main(String arg[]) { // the actual code } }

A[i] = Math.sqrt(i+1);

First, declare the array, allocate memory, and put the 20 square roots into the array | |

Then, compute the sum of the 20 square roots, perhaps in another loop. You should use
shortcut notation like sum += A[i]; | |

Next, compute the average by dividing the sum by 20 | |

Finally, in another loop yet, print out the differences between the square roots and the average using the System.out.println() method. |