04 From Python To Java
04 From Python To Java
CISC124
1
Lists (and other sequences) in Python use [] for
indexing (and slicing)
2
Arrays
in Java, an array is a container object that holds a fixed
number of values of a single type
the length of an array is established when the array is
created
3
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Arrays
to declare an array, you use the element type followed
by an empty pair of square brackets
double[] collection;
// collection is an array of double values
4
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
A Java array is (far) less flexible than a Python list, but
arrays also use [] for indexing
no slicing of arrays
no negative indexing (causes a runtime error)
element type must be specified
5
An array can initialize its size and elements by writing
the elements in a comma separated list
but this can only be done when the array variable is
declared
char[] u;
u = {'x', 'y', 'z'}; // oops, assignment
// syntax error
6
Arrays are normally created using the new operator and
specifying the capacity or length of the array
the capacity is the maximum number of elements that
the array can hold
capacity appears in square brackets after the element
type
double[] collection;
// collection is an array of double values
7
Arrays of primitive type elements have all the elements
initialized to a default value when using new
import java.util.Arrays;
8
Default values for arrays
9
To fill an existing array so that all elements have the
same value, use the method Arrays.fill
import java.util.Arrays;
10
Arrays
the number of elements in the array is stored in the
public field named length
double[] collection;
// collection is an array of double values
int n = collection.length;
// the public field length holds the number of elements
11
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Arrays
the values in an array are called elements
the elements can be accessed using a zero-based index
(no negative indexing)
12
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Arrays
the elements can be accessed using a zero-based index
(similar to strings)
13
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
A common operation when working with Python lists is
to loop over the elements of the list
e.g., to manually find the maximum value in a non-
empty list
def max_elem(t):
hi = t[0]
for elem in t:
if elem > hi:
hi = elem
return hi
14
Java's for-each loop is similar to Python's:
15
In many situations, you want the loop variable to
correspond to the index of the element
e.g., to find index of the maximum value in a non-
empty list
def max_index(t):
index = 0
hi = t[index]
for i in range(0, len(t)):
if t[i] > hi:
index = i
hi = t[i]
return hi
16
Java has a second kind of for loop that is suitable for an
index-based loop
was the only kind of for loop until Java 5
17
for loops
a regular for loop has four main parts
1. an initialization expression
2. a termination condition
3. an update expression
4. a loop body
parts 1, 2, and 3 are technically all optional but it is a little
unusual to see a for loop that does not have all four parts
the two semi-colons separating parts 1 and 2 and 2 and 3 are
always required
18
The initialization expression initializes the loop
executed exactly once when the loop begins
usually, a loop variable is declared and initialized in
the initialization expression
19
The termination expression must evaluate to true for
the loop to run the next iteration
executed before every loop iteration
if it evaluates to false then the loop stops iterating
20
The update expression is evaluated after each iteration of
the loop
almost always modifies the value of the loop variable
for (int i = 0; i < arr.length; i++) { in this situation, i++ has the
same effect as i = i + 1
21
If the loop body must access more than one element of
an array in each iteration, then you should probably use
a regular for loop instead of a for-each loop
e.g., test if an array is sorted in ascending order
22
iteration 1
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
iteration 2
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
iteration 3
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
iteration 14
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
iteration 15
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
The break statement will immediately break out of the
loop causing program flow to proceed to the statement
immediately after the loop.
28
iteration 3, break statement runs stopping the loop
12 13 99 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i-1 i
If the loop body must access more than one element of
an array in each iteration, then you should probably use
a regular for loop instead of a for-each loop
e.g., reverse the order of the elements in an array
30
iteration 1
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i j
iteration 2
27 13 14 15 16 17 18 19 20 21 22 23 24 25 26 12
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i j
iteration 3
27 26 14 15 16 17 18 19 20 21 22 23 24 25 13 12
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i j
iteration 8
27 26 25 24 23 22 21 19 20 18 17 16 15 14 13 12
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i j
iteration 8 ends
27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i j
In the previous example, there are actually two loop
variables of the same type
it is possible to incorporate both loop variables into
the initialization and update expressions
36
If the loop body must access elements from two or more
arrays, then you must use a regular for loop
e.g., return a new array by summing corresponding
elements of two input arrays
37
In Python, you can slice a list to get the elements located
in a sublist of the original list:
38
Java has no built-in slice operator, but you can write your
own method to slice an array:
public static int[] slice(int[] arr, int start, int stop) {
if (start > stop) {
throw new IllegalArgumentException("start > stop");
}
// what other input errors do we need to check for?
39
Calling the Java version of slice:
40
In Python, you can (shallow copy) a list by slicing the
entire list:
41
In Java, you could use slice, but arrays have a clone()
method that you can use instead:
42