Arrays
Arrays
Multidimensional Arrays
Array Declaration
•
int[] someArray;
–
Declares an integer array
–
Brackets after the type = preferred
•
int someArray[];
–
Declares an integer array
–
Brackets after the variable name = not preferred
•
int someArray = new int[10];
–
Declares an array of size 10
–
Primitive types are initialized to their default values
•
int[] someArray = {1,2,3,4,5};
–
Declares and initializes an array for five integers
Default Values
•
int = 0
•
Boolean = false
•
Char = \u0000
•
Objects = null
Length of Array
•
int someArray[] = new int[10];
•
someArray.length returns 10
Array Example
int someArray[] = new int[10];
0 0 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
Array Example
int someArray[] = new int[10];
0 0 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
6
Array Example
int someArray[] = new int[10];
0 0 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
7
Array Example
int someArray[] = new int[10];
0 0 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
8
Indices
int someArray[] = new int[10];
0 0 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
9
Indices
int someArray[] = new int[10];
0 5 0 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
10
Indices
int someArray[] = new int[10];
0 5 5 0 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
11
Indices
int someArray[] = new int[10];
0 5 5 10 0 0 0 0 0 0
Index 0 1 2 3 4 5 6 7 8 9
Length 10
12
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5];
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 14 Index Value 0 0 1 0 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 15 Index Value 0 0 1 0 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 16 Index Value 0 0 1 0 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4]; } } 17 Index Value 0 0 1 1 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 18 Index Value 0 0 1 1 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 19 Index Value 0 0 1 1 2 0 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4]; } } 20 Index Value 0 0 1 1 2 3 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 21 Index Value 0 0 1 1 2 3 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 22 Index Value 0 0 1 1 2 3 3 0 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4]; } } 23 Index Value 0 0 1 1 2 3 3 6 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 24 Index Value 0 0 1 1 2 3 3 6 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 25 Index Value 0 0 1 1 2 3 3 6 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
}
values[0] = values[1] + values[4]; } } 26 Index Value 0 0 1 1 2 3 3 6 4 10 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 27 Index Value 0 0 1 1 2 3 3 6 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4]; } } 28 Index Value 0 0 1 1 2 3 3 6 4 0 Array
Simple Array Example
public class Test {
public static void main(String[] args) {
int[] values = new int[5]; for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1]; }
values[0] = values[1] + values[4];
} } 29 Index Value 0 11 1 1 2 3 3 6 4 10 Array
Useful Things to do with Arrays
•
Print all elements
•
Sum all elements
•
Find the largest element
•
Shift elements
Print All Elements
for (int i = 0; i < myArray.length; i++)
{
System.out.print(myArray[i] + " ");
}
Sum All Elements
double total = 0;
for (int i = 0; i < myArray.length; i++)
{
total += myArray[i];
}
Find Largest Element
//should check that there is at least one element
double max = myArray[0];
for (int i = 1; i < myArray.length; i++)
{
if (myArray[i] > max)
{
max = myArray[i];
}
}
Shifting Elements
double temp = myArray[0];
for(int i=0; i<myArray.length-1; i++)
{
myArray[i] = myArray[i+1];
}
myArray[myArray.length-1] = temp;
Random Shuffle
•
For each array element
–
Choose random number
–
Swap array[i] and array[randomNumber]
Random Shuffle
//initialize array up here
Random random = new Random(0);
for(int i=0; i<myArray.length; i++)
{
int randomSwap = random.nextInt(myArray.length);
int temp = myArray[i];
myArray[i] = myArray[randomSwap];
myArray[randomSwap] = temp;
}
Example – RandomShuffle.java
•
RandomShuffle.java
Iterate Over All Elements
•
Iterate through all elements in order
•
Different type of for loop syntax
for(double value: myArray)
{
System.out.println(value);
}
Iterate Over All Elements
•
Iterate through all elements in order
•
Different type of for loop syntax
for(double value: myArray)
{
System.out.println(value);
}
39
Iterate Over All Elements
•
Iterate through all elements in order
•
Different type of for loop syntax
for(double value: myArray)
{
System.out.println(value);
}
40
Iterate Over All Elements
•
Iterate through all elements in order
•
Different type of for loop syntax
for(double value: myArray)
{
System.out.println(value);
}
41
Copying Arrays
42
Array Contents
array1
Array Contents
array2
Shallow Copy
43
Array Contents
array1
Array Contents
array2
array2 = array1;
Nothing pointing to array2
array2 = garbage
Deep Copy
44
Array Contents
array1
Array Contents
array2
for(int i=0; i<array1.length; i++) {
array2[i] = array1[i]; }
Arraycopy
•
System.arraycopy
–
https://
docs.oracle.com/javase/8/docs/api/java/lang/Syste
m.html#arraycopy-java.lang.Object-int-java.lang.
Object-int-int-–
public static void arraycopy
• (Object src, int srcPos, Object dest, int destPos, int length)
•
Arrays.copyOf
–
https://
docs.oracle.com/javase/8/docs/api/java/util/Arrays.
html
Searching Arrays
•
Linear Search
–
For unsorted arrays
•
Binary Search
–
For sorted arrays
–
Arrays.binarySearch
•
https://
docs.oracle.com/javase/8/docs/api/java/util/Arrays.ht
ml
Example – SearchExamples.java
•
SearchExamples.java
Sorting Arrays
•
Arrays.sort
–
https://
docs.oracle.com/javase/8/docs/api/java/util/Arrays.
html
•
Uses dual pivot quicksort for primitive types
•
Uses Timsort for Objects
–
Merge sort + Insertion sort hybrid
Example – SortComparison.java
•
SortComparison.java
Problem
•
Given an unsorted array of numbers determine
if the array contains a given value for each
value in a large list
•
Do not modify the original array
–
It is important not to lose the original data
Problem
•
What needs to be done?
–
Can do linear search for each value
•
Will be slow for a large number of values
–
Can do binary search for each value
•
Requires the list to be sorted
Possible Solution #1
•
Copy the original array
•
Sort the copied array
•
Search for each value in the copied array
Possible Solution #2
•
Copy the array
•
Sort the copied array
•
Copy the list of values
•
Sort the copied list of values
•
Use binary search
–
Change the minimum starting element based on
the results of the previous search
Example – ValueSearch.java
•
ValueSearch.java
•
Solution for solution #1
Multidimensional Arrays
Single Array
•
Can be thought of as a list
•
{1, 2, 3, 4, 5,}
–
1
–
2
–
3
–
4
–
5
2D Array
•
Can be thought of as a table
x
x
2x
3x
4x
51
1
1
1
1
2
4
8
16
32
3
9
27
81
243
4
16
64
256
1024
5
25
125
625
3125
6
36
216
1296
7776
7
49
343
2401
16807
8
64
512
4096
32768
9
81
729
6561
59049
10
100 1000 10000 100000
N-dimensional array
•
How to visualize this?
•
Picture an array or rectangles
–
And rectangles inside each of those rectangles
•
And rectangles inside each of those rectangles
– Etc…
•
Treemap
59
One-dimensional array
One Dimensional Array (int[] x = new int[3]) x[1]
60
Two Dimensional Array (int[][] x = new int[3][3])
x[0] x[1] x[2]
x[1][1]
x[1][2]
x[2][1]
x[2][2]
x[1][0] x[2][0]
x[0][1]
x[0][2] x[0][0]
61
Three-dimensional array
x[0][1][2]
x[0][0] x[1][0] x[2][0]
x[0] x[1] x[2]
Three Dimensional Array (int[][][] x = new int[3][3][3])
Example – SpaceSniffer.exe
•
SpaceSniffer.exe
•
Program to visualize what contents are using
your disk space
•
Another example
–
https://
developers.google.com/chart/interactive/docs/galle
ry/treemap#example
Declaring and Creating
Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;
// Create array and assign its reference to variable
refVar = new dataType[10][10];
// Combine declaration and creation in one statement
dataType[][] refVar = new dataType[10][10];
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
Two-Dimensional Array Example
int[][] array = new int[5][5];
64
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
0
2
0
0
0
0
0
3
0
0
0
0
0
Two-Dimensional Array Example
array[1][4] = 222;
65
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
222
2
0
0
0
0
0
3
0
0
0
0
0
Two-Dimensional Array Example
int[][] array = new int[5][];
66
Two-Dimensional Array Example
array[0] = new int[5];
67
0
1
2
3
4
0
0
0
0
0
0
Two-Dimensional Array Example
array[1] = new int[4];
68
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
Two-Dimensional Array Example
array[2] = {1, 2, 3};
69
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
2
1
2
3
Two-Dimensional Array Example
int[][] array = {{1, 2}, {3, 4}, {5, 6, 7}}
70
0
1
2
0
1
2
1
3
4
Useful Things to do with
multi-dimensional Arrays
•
Print all elements
•
Sum all elements
•
Find the largest element
•
Shift elements
Print All Elements
72
for (int i = 0; i < myArray.length; i++)
{
for(int j=0; j<myArray[i].length; j++)
{
System.out.print(myArray[i][j] + " ");
}
Sum All Elements
73
double total = 0;
for (int i = 0; i < myArray.length; i++)
{
for(int j=0; j<myArray[i].length; j++)
{
total += myArray[i][j];
}
Find Largest Element
double max = myArray[0][0];
for (int i = 0; i < myArray.length; i++)
{
for(int j=0; j<myArray[i].length; j++)
{
if (myArray[i][j] > max)
{
max = myArray[i][j];
}
}
}
Shifting Elements
double temp = myArray[0][0];
for(int i=0; i<myArray.length-1; i++)
{
double temp2 = myArray[i][0];
for(int j=0; j<myArray[i].length-1; j++)
{
myArray[i][j] = myArray[i][j+1];
}
myArray[i][myArray[i].length-1] = temp2;
}
myArray[myArray.length-1][myArray[i].length-1] = temp;
Random Shuffle 2D Array
•
Need to loop through all elements
–
Need to choose a random position to swap with
Random Shuffle 2D Array
//initialize array and “random” variable up here
for(int i=0; i<myArray.length; i++)
{
for(int j=0; j<myArray[i].length; j++)
{
int randomI = random.nextInt(myArray.length);
int randomJ = random.nextInt(myArray.length);
int temp = myArray[i][j];
myArray[i][j] = myArray[randomI][randomJ];
myArray[randomI][randomJ] = temp;
}
}