Joseph Haugh
University of New Mexico
double x1 = 13;
double x2 = 27.3;
double x3 = 32.91;
double x4 = 103.4;
double sum = x1 + x2 + x3 + x4;
double average = sum / 4;
double x1 = 13;
double x2 = 27.3;
double x3 = 32.91;
double x4 = 103.4;
double x5 = 73;
double x6 = 90;
double x7 = 22;
double x8 = 4.31;
double x9 = 3.14;
double x10 = 90.45;
double sum = x1 + x2 + x3 + x4 + x5
+ x6 + x7 + x8 + x9 + x10;
double average = sum / 10;
double x1 = 13;
double x2 = 27.3;
double x3 = 32.91;
double x4 = 103.4;
double x5 = 73;
double x6 = 90;
double x7 = 22;
double x8 = 4.31;
double x9 = 3.14;
double x10 = 90.45;
double sum = x1 + x2 + x3 + x4 + x5
+ x6 + x7 + x8 + x9 + x10;
double average = sum / 10;
An arrays allow us to hold multiple pieces of data inside 1 variable
They are similar to lists in Python
They are 0-based meaning the index of the first element is 0
Arrays length is immutable
Creation syntax:
<<typeName>>[] <<variableName>> = new <<typeName>>[<<arraySize>>];For example:
double[] xs = new double[10];In English: create an array of doubles named xs which can hold 10 elements.
double[] xs = new double[10];
xs[0] = 13;
xs[1] = 27.3;
xs[2] = 32.91;
xs[3] = 103.4;
xs[4] = 73;
xs[5] = 90;
xs[6] = 22;
xs[7] = 4.31;
xs[8] = 3.14;
xs[9] = 90.45;
double sum = 0;
for(int i = 0; i < xs.length; i++) {
sum += xs[i];
}
double average = sum / xs.length;
double[] xs = new double[10];
xs which can hold 10 elementsxs[0] = 13;
xs assign index 0 to the value 13for(int i = 0; i < xs.length; i++)
i from 0 to the length of xs increment i by 1 after10sum += xs[i];
i in xs to sumdouble average = sum / xs.length;
sum by the length of xs and assign it to average// Leaves the value empty,
// you can declare it later
int[] a;
// int array of size 4
int[] b = new int[4];
// String array of size 3
String[] c = new String[3];
// int array of length 4
int[] xs = { 5, 3, 8, 4 };
// String array of length 2
String[] strs = { "hello", "world" };
-1int findLargest(int[] xs) {
// Initialize largest to -1
// Loop over each element of the array
// Determine if current element is the largest
// Return the largest
}
int findLargest(int[] xs) {
// Initialize largest to -1
int largest = -1;
// Loop over each element of the array
for (int i = 0; i < xs.length; i++) {
// Determine if current element is the largest
}
// Return the largest
return largest;
}
int findLargest(int[] xs) {
// Initialize largest to -1
int largest = -1;
// Loop over each element of the array
for (int i = 0; i < xs.length; i++) {
// Determine if current element is the largest
if (xs[i] > largest) {
largest = xs[i];
}
}
// Return the largest
return largest;
}
int findLargest(int[] xs) {
// Initialize largest to -1
int largest = -1;
// Loop over each element of the array
for (int i = 0; i < xs.length; i++) {
// Determine if current element is the largest
if (xs[i] > largest) {
largest = xs[i];
}
}
// Return the largest
return largest;
}
void main() {
int[] xs = { 1, 2, 3, 4, 5, 1, 2, 3, 6, 1, 1 }; // 6
IO.println(findLargest(xs));
}
void main() {
// length 3, indices 0 - 2
int[] xs = new int[3];
xs[0] = 2;
xs[1] = 3;
xs[2] = 4;
// This will cause an error
xs[3] = 5;
}
void main() {
int len = 3;
int[] xs = new int[len];
for (int i = 0; i < len; i++) {
xs[i] = i + 1;
}
// empty to start
int[] ys = new int[len * 2];
// copy over
for (int i = 0; i < len; i++) {
ys[i] = xs[i];
}
// now we have indices 3 - 5 open
}
Consider the following code:
void main() {
int[] xs = {1, 2, 3};
int[] ys = {4, 5, 6, 7};
ys = xs;
// What will this print?
IO.println(ys.length);
// What about this?
IO.println(ys[0]);
}
Code
int x = 1;
Representation

Code
int x = 1;
int y = 2;
Representation

Code
int x = 1;
int y = 2;
y = x;
Representation

Code
int x = 1;
int y = 2;
y = x;
int[] xs = {1, 2, 3};
Representation

Code
int x = 1;
int y = 2;
y = x;
int[] xs = {1, 2, 3};
int[] ys = {4, 5, 6, 7};
Representation

Code
int x = 1;
int y = 2;
y = x;
int[] xs = {1, 2, 3};
int[] ys = {4, 5, 6, 7};
ys = xs;
Representation

For example, what will this code print?
void foo(int x) {
x = 5;
}
void main() {
int x = 4;
foo(x);
IO.println(x);
}
Prints: 4
Code
void foo(int x) {
x = 5;
}
void main() {
int x = 4;
foo(x);
IO.println(x);
}
Representation

What will this code print?
void foo(int[] xs) {
xs[0] = 5;
}
void main() {
int[] xs = {1, 2, 3};
foo(xs);
IO.println(xs[0]);
}
Prints: 5
void foo(int[] xs) {
xs[0] = 5;
}
void main() {
int[] xs = {1, 2, 3};
foo(xs);
IO.println(xs[0]);
}
Representation

void doubleAll(int[] xs) {
for (int i = 0; i < xs.length; i++) {
xs[i] = xs[i] * 2;
}
}
void main() {
int[] xs = {1, 2, 3};
doubleAll(xs);
// prints something like: [I@3a71f4dd
IO.println(xs);
}
Custom Printer:
String showIntArray(int[] xs) {
if (xs.length == 0) {
return "[]";
}
String s = "[" + xs[0];
for (int i = 1; i < xs.length; i++) {
s += ", " + xs[i];
}
s += "]";
return s;
}
void main() {
int[] xs = {1, 2, 3};
IO.println(showIntArray(xs));
}
Arrays.toString()Preferred way:
void main() {
int[] xs = {1, 2, 3};
IO.println(Arrays.toString(xs));
}
Math.<<functionName>>Math.min(<<number1>>, <<number2>>)Math.max(<<number1>>, <<number2>>)Math.abs(<<number>>)Math.round(<<number>>)Math.sin(<<number>>)The ternary operator is an if expression
Ternary syntax:
<<booleanCondition>> ? <<thenValue>> : <<elseValue>>;Without ternary
int min(int x, int y) {
int result;
if (x < y) {
result = x;
} else {
result = y;
}
return result;
}
With ternary
int min(int x, int y) {
int result = x < y ? x : y;
return result;
}