CS计算机代考程序代写 compiler Java COMP 250

COMP 250
INTRODUCTION TO COMPUTER SCIENCE
Week 2-2: Arrays
Giulia Alberini, Fall 2020

WHAT ARE WE GOING TO DO IN THIS VIDEO?
 Arrays Multidimensional arrays

ARRAYS

ARRAYS
 An array is like a container that holds a fixed number of values of the same type. This values can be access using the same variable name.
 The length of the array is established when the array is created and it cannot be changed.
 All the elements of an array must be of the same type.  The values in the array are called elements.
 The list of elements is indexed, the order matters.

ARRAYS – DECLARATION
To create an array, we first have to declare a variable with an array type:
type[] variable_name;

ARRAYS – DECLARATION
To create an array, we first have to declare a variable with an array type:
type[] variable_name;
The square brackets indicate that this is an array variable

ARRAYS – DECLARATION
To create an array, we first have to declare a variable with an array type:
variable_name;
type[]
The square brackets indicate that this is an array variable
All the elements of the array must have this type.

ARRAYS – DECLARATION
 To create an array, we first have to declare a variable with an array type:
 Examples:
type[] variable_name;
String[] days;
int[] grades;
double[] heights;
String[] args;

ARRAYS – DECLARATION
 To create an array, we first have to declare a variable with an array type:
type[] variable_name;
NOTE: The declaration creates a variable, but we still need to create the actual array object itself!
 Examples:
String[] days;
int[] grades;
double[] heights;
String[] args;

ARRAYS – EXAMPLE
Here is a list of the days of the week:
0. Monday
1. Tuesday
2. Wednesday 3. Thursday 4. Friday
5. Saturday
6. Sunday
We would like to use an array to store them all.
Note that we start counting at 0!!

ARRAYS – INITIALIZATION METHOD 1
 Using curly brackets, we can assign values to all elements in the array at the same time as we declare the array:
String[] days = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};
 days is an array of String values of length 7.
 Order matters!! What is the index of “Tuesday”?
 This method does not work if you don’t know the length or the values of the array before the program runs.

ARRAYS – INITIALIZATION METHOD 2
 We can use the new operator to create an array of a certain size. We can then populate the entries one at a time, later in the program.

ARRAYS – INITIALIZATION METHOD 2
 We can use the new operator to create an array of a certain size. We can then populate the entries one at a time, later in the program.
 The following examples all create an array of String values with length 7.
String[] days = new String[7];
int numberOfDays = 7;
String[] days;
days = new String[numberOfDays];
String[] days;
days = new String[7];

ARRAYS – INITIALIZATION METHOD 2
 We can use the new operator to create an array of a certain size. We can then populate the entries one at a time, later in the program.
 After the creation, we can populate the entries
String[] days = new String[7];
days[1] = “Tuesday”;
days[0] = “Monday”;
days[6] = “Sunday”;
days[5] = “Saturday”; days[2] = “Wednesday”; days[3] = “Thursday”; days[4] = “Friday”;

DEFAULT VALUES
 As soon as we create an array object and initialize the variable, java assigns default values to each position in the array. Thus, if we don’t assign any values to the array, it will have the values assigned by default.
 The default values are:
 For numerical arrays: 0
 For String/reference type arrays: null
 For char arrays: the character with ASCII value 0  For boolean arrays: false.

SIZE OF AN ARRAY
 When we create an array of size n, n contiguous places in memory are reserved in order to store n values of the same type.
 The size of an array cannot be changed after it has been assigned.
 Any integer expression can be used for the size of an array as long as the value is nonnegative. If negative: NegativeArraySizeException.

ACCESSING ELEMENTS
 Elements in an array can be access using the name of the array variable and the index of the element inside square brackets.
days[0] = “Sunday”;
The value “Sunday” is assigned to days at index 0
String lastDay = days[6];
The value in days at index 6 is assigned to the variable lastDay
days[0] = days[6];
The value in days at index 6 is assigned to days at index 0.
 If we use an index that is negative or grater than the size of the array minus 1, the result is an ArrayIndexOutOfBoundsException.

ARRAYS AND LOOPS
 When we wanted to traverse a String s, we would loop through all the indices up to (and not including) s.length()
 Similarly, we can access the length of an array using grades.length
double sum = 0;
for (int i = 0; i < grades.length; i++) { sum += grades[i]; } double avg = sum/grades.length; ARRAYS AND LOOPS  When we wanted to traverse a String s, we would loop through all the indices up to (and not including) s.length()  Similarly, we can access the length of an array using grades.length double sum = 0; for (int i = 0; i < grades.length; i++) { sum += grades[i]; } double avg = sum/grades.length; Note the difference! COMMON MISTAKES Forgetting to initialize the entries of your array. int[] a = new int[5]; System.out.println(a[0]); Output: 0 COMMON MISTAKES Referring to an index that doesn't exists. String[] petNames = {"Rex", "Sparky", "Bilbo", "Small cat"}; System.out.println(petNames[5]); System.out.println(petNames[-2]);  ArrayIndexOutOfBounds exception COMMON MISTAKES  Trying to create an array with negative size. int size = -3 % 5; String[] petNames = new String[size];  NegativeArraySizeException exception PRINTING DIFFERENCES public static void myMethod(int[] a) { for(int i=0; i