0% found this document useful (0 votes)
6 views41 pages

Understanding Java Arrays Basics

The document provides an overview of Java arrays, explaining their fixed size, memory allocation, and how they differ from Python lists. It covers array declaration, reference handling, and methods for processing arrays, including copying and growing arrays. Additionally, it discusses two-dimensional arrays and their applications, such as maintaining a game board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views41 pages

Understanding Java Arrays Basics

The document provides an overview of Java arrays, explaining their fixed size, memory allocation, and how they differ from Python lists. It covers array declaration, reference handling, and methods for processing arrays, including copying and growing arrays. Additionally, it discusses two-dimensional arrays and their applications, such as maintaining a game board.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Arrays

Computer Science OOD


Boston University
Christine Papadakis
Arrays

An Array is a fixed data structure.

It is a container that holds


a fixed number of elements of the same data type.

The length of an array is established when


the array is declared and
the physical size of the array
cannot be altered during run time.
Arrays

Specifically:

An array variable references


a contiguous memory allocation of
some fixed number of elements
of the same data type.
Sequences
Why isin Python
this and Java
important?
• In Python, a list is a sequence of arbitrary values.
This is why arrays are more efficient and
[2, 4, 6, 8]
require less overhead than Python lists!
['CS', 'math', 'english', 'psych']
• the elements can
Contiguous have arbitrary
allocation types:
means that all the elements
of the array
['Star are stored
Wars', 1977, in 'PG',
consecutive
[35.9,memory cells.
460.9]]

The fact that all the elements are of the exact data type means
• Java
that provides a similar
each element constructthe
is allocated known
sameas an array.
number of bytes.
• the elements must have the same data type
Therefore if we know
• less flexible than athe address
list, location
with less built-inoffunctionality
the first element,
• we canhas
it also create
lessan offset from that starting address and
overhead
directly access every element.
• example: a Java array of 1000 integers
will use much less memory than
a Python list of 1000 integers
• it is easier to use it efficiently
Array Variables
• We use a variable to represent the array as a whole.

• Example of declaring an array variable:


int[] temps;
• the [] indicates that variable temps represents an array
• the int indicates that the elements will be of type int
• note that the array itself has not been allocated, only the
variable that will reference the array has been declared.
Array Variables
• We use a variable to represent the array as a whole.

• Example of declaring an array variable:


int[] temps = {1, 2, 3, 4, 5};
• the [] indicates that variable temps represents an array
• the int indicates that the elements will be of type int
• creates an array in memory, initialized with the specified
elements, and assigns the memory location (of the first element)
to variable temps.
Array Variables
• We use a variable to represent the array as a whole.

• Example of declaring an array variable:


int[] temps = new int[5];
• the [] indicates that variable temps represents an array
• the int indicates that the elements will be of type int
• creates an array of five elements (initialized to default value) in
memory and assigns the memory location (of the first element)
to variable temps.
Arrays and References
• An array variable does not store the array itself.

• It stores a reference to the array.


• the memory address of the array
int[] temps = {51, 50, 36, 29, 30};

temps 51 50 36 29 30

temps[ [Link]-1 ]

[ offset from the starting address location of the array ]


Arrays and References
• An array variable does not store the array itself.

• It stores a reference to the array.


• the memory address of the array
int[] temps = {51, 50, 36, 29, 30};

Stack
temps Heap
51 50 36 29 30

temps 8002 51 50 36 29 30

specifically, contains the


address of the first element
of the array!
Arrays and References:
a more accurate visual
4000
• An array variable does not store the array itself.
4001
temp 8002
• It stores a reference
4002 to the array.
• the memory4003 address of the array Stack
int[] temps = {51, 50, 36, 29, 30};
Stack Heap
temps 51 50 36 29 30

Assume
2 bytes
per
8000
integer
8001
8002
array memory
8003

8004
Heap
Arrays and References:
a more accurate visual
4000
• An array variable does not store the array itself.
4001
temp 8002
• It stores a reference
4002 to the array.
• the memory4003 address of the array Stack
int[] temps = {51, 50, 36, 29, 30};
Stack Heap
temps 51 50 36 29 30

temp[0]

8000

8001
8002
array memory
8003

8004
Heap
Arrays and References:
a more accurate visual
4000
• An array variable does not store the array itself.
4001
temp 8002
• It stores a reference
4002 to the array.
• the memory4003 address of the array Stack
int[] temps = {51, 50, 36, 29, 30};
Stack Heap
temps 51 50 36 29 30

temp[1]

8000

8001
8002
array memory
8003

8004
Heap
Arrays and References
• An array variable does not store the array itself.

• It stores a reference to the array.


• the memory address of the array
int[] temps = {51, 50, 36, 29, 30};

temps 51 50 36 29 30

• If we print an array variable, we print the contents or value of the


variable, which is the memory address of the array!
[Link](temps);
output:
[I@1e1fd124
Arrays and References
• An array variable does not store the array itself.

• It stores a reference to the array.


• the memory address of the array
int[] temps = {51, 50, 36, 29, 30};

temps 51 50 36 29 30

• To print the contents of the array, we must first invoke a static


method of the Array class that returns a string representation of
the specified array:

[Link]( [Link](temps) );
Processing a Sequence Using a Loop
Index-based:

for (int i = 0; i < [Link]; i++) {


do something with array[i] where array is the array variable
}

Element-based:

for (int val: array) {


do something with val where array is the array variable
}

• Index-based is more flexible:


• you can use it to change the element with index i
• you can keep track of where you saw a given value
Array Assignment
Copying a Reference Variable
• What does this do?
int[] values = {7, 8, 9, 6, 10, 7, 9, 5};
int[] other = values;

values 7 8 4 6 10 7 9 5
other

• Given the lines of code above, what will the lines below print?
other[2] = 4;
[Link](values[2] + " " + other[2]);
4 + " " + 4
"4 4"
Shallow Copy
Copying an Array
• To actually create a deep copy of an array, we can:
• create a new array of the same length as the first
• traverse the arrays and copy the individual elements

• Example:
int[] values = {7, 8, 9, 6, 10, 7, 9, 5};
int[] other = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
other[i] = values[i];
}
i: 7
values 7 8 9 6 10 7 9 5
other 7 8 9 6 10 7 9 5
Example: Memory layout
Growing an Array
int[] values = {7, 8, 9, 6, 10, 7, 9, 5};
...
int[] temp = new int[16];
for (int i = 0; i < [Link]; i++) {
temp[i] = values[i];
}

7 8 9 6 10 7 9 5
values

7 8 9 6 10 7 9 5 0 0 0 0 0 0 0 0

temp
Example: Memory Layout This
Growing an Array memory remains
“in limbo”
int[] values = {7, 8, 9, 6, 10, 7, waiting
9, 5}; for the
... garbage collector
int[] temp = new int[16]; to free it!
for (int i = 0; i < [Link]; i++) {
temp[i] = values[i];
}

7 8 9 6 10 7 9 5
values

7 8 9 6 10 7 9 5 0 0 0 0 0 0 0 0

temp

// re-assign the temporary array


values = temp
Example: Memory Layout
Growing an Array
int[] values = {7, 8, 9, 6, 10, 7, 9, 5};
...
int[] temp = new int[16];
for (int i = 0; i < [Link]; i++) {
temp[i] = values[i];
}

7 8 9 6 10 7 9 5
values

7 8 9 6 10 7 9 5 0 0 0 0 0 0 0 0

temp

// re-assign the temporary array


values = temp
Example: Memory Layout
Growing an Array
int[] values = {7, 8, 9, 6, 10, 7, 9, 5};
...
int[] temp = new int[[Link]*2];
for (int i = 0; i < [Link]; i++) {
temp[i] = values[i];
}

7 8 9 6 10 7 9 5
values

7 8 9 6 10 7 9 5 0 0 0 0 0 0 0 0

temp

// re-assign the temporary array


values = temp
A 2-D Array
• A 2-D array, an array of arrays

• To declare a 2-D array we must specify both the row and the
column dimension as:

int[][] twoDim = new int [5][4];

• This statement is equivalent to:


int[][]twoDim = new int[5][]; int[][]twoDim;
twoDim[0] = new int[4]; twoDim = new int[5][4];
twoDIm[1] = new int[4];
twoDim[2] = new int[4];
A 2-D Array

Stack Heap

twoDim

12

twoDim[2][1] = 12;
Maintaining a Game Board
• For a simple Tic-Tac-Toe board, we could use a 2-D array to
keep track of the state of the board:
char[][] board = new char[3][3];

• Alternatively, we could create and initialize it as follows:


char[][] board = {{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};

• If a player puts an X in the middle square, we could record


this fact by making the following assignment:
board[1][1] = 'X';
Processing All of the Elements in a 2-D Array
• To perform some operation on all of the elements in a 2-D
array, we typically use a nested loop.
• example: finding the maximum value in a 2-D array.
public static int maxValue(int[][] arr) {
int max = arr[0][0];
for (int r = 0; r < [Link]; r++) {
for (int c = 0; c < arr[r].length; c++) {
if (arr[r][c] > max) {
max = arr[r][c];
}
}
}
return max;
}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 11 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 11 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 11 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { i 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { i 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { i 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 2
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { i 0 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
} ++a[i]
b[i] = a[i]
public static void main(String[] args) { i 0 0
int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
} ++a[i]
b[i] = a[i]
public static void main(String[] args) { i 0 0
int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 4
int[] b = a; a
6
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 8
} args
}

public static void main(String[] args) { i 0 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 5
int[] b = a; a
7
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 9
} args
}

public static void main(String[] args) { i 0 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 5
int[] b = a; a
7
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 9
} args
}

public static void main(String[] args) { i 0 0


int i = s_var++;
int[] a = {2, 4, 6, 8};
b 0
int[] b = new int[[Link]]; a
int[] c = a; 0
ret
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 c 3
public static void foo(int[] a) {
s_var--; b 5
int[] b = a; a
7
for (int i = 0; i < [Link]; i++) { i 10
b[i] = ++a[i]; 9
} args
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
s_var 10 3
public static void foo(int[] a) {
s_var--; 5
int[] b = a;
7
for (int i = 0; i < [Link]; i++) {
b[i] = ++a[i]; 9
}
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
3
public static void foo(int[] a) {
s_var--; 5
int[] b = a;
7
for (int i = 0; i < [Link]; i++) {
b[i] = ++a[i]; 9
}
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;
3
public static void foo(int[] a) {
s_var--; 5
int[] b = a;
7
for (int i = 0; i < [Link]; i++) {
b[i] = ++a[i]; 9
}
}

public static void main(String[] args) { 0


int i = s_var++;
int[] a = {2, 4, 6, 8}; 0
int[] b = new int[[Link]];
int[] c = a; 0
foo(a); 0
}

}
Understanding Java Arrays

public class ArrayTest { Static Stack Heap


static int s_var = 10;

public static void foo(int[] a) {


s_var--;
int[] b = a;

for (int i = 0; i < [Link]; i++) {


b[i] = ++a[i];
}
}

public static void main(String[] args) {


int i = s_var++;
int[] a = {2, 4, 6, 8};
int[] b = new int[[Link]];
int[] c = a;

foo(a);
}

You might also like