Loading subject…
An array is a fixed-size, ordered collection of elements that all share the same data type.
Python (a List used as a fixed row):
numbers = [1, 2, 3, 4]print(numbers[0]) # 1 -> the first elementprint(numbers[3]) # 4 -> the last elementprint(len(numbers)) # 4 -> how many elementsJava:
int[] numbers = {1, 2, 3, 4};int[] scores = new int[10]; // 10 slots, all holding 0
System.out.println(numbers[0]); // 1 -> the first elementSystem.out.println(numbers[3]); // 4 -> the last elementSystem.out.println(numbers.length); // 4 -> how many elementsPython:
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[1]) # [4, 5, 6] -> the second rowprint(matrix[1][0]) # 4 -> row 1, column 0print(len(matrix)) # 2 -> number of rowsprint(len(matrix[0])) # 3 -> number of columnsJava:
int[][] matrix = { {1, 2, 3}, {4, 5, 6} };
System.out.println(matrix[1][0]); // 4 -> row 1, column 0System.out.println(matrix.length); // 2 -> number of rowsSystem.out.println(matrix[0].length); // 3 -> number of columnsPython, totalling every value in a table:
matrix = [[1, 2, 3], [4, 5, 6]]
total = 0for row in range(len(matrix)): for col in range(len(matrix[row])): total = total + matrix[row][col]
print(total) # 21Java, the same algorithm:
int[][] matrix = { {1, 2, 3}, {4, 5, 6} };
int total = 0;for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[row].length; col++) { total = total + matrix[row][col]; }}
System.out.println(total); // 21A List is an ordered, resizable collection of elements.
Python list:
fruits = ["apple", "banana"]
fruits.append("cherry") # add -> ['apple', 'banana', 'cherry']fruits.insert(1, "kiwi") # add at index 1fruits.remove("banana") # remove by value
for f in fruits: # traverse print(f)
print(len(fruits)) # 3Java ArrayList:
import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple"); // addfruits.add("banana");fruits.add(1, "kiwi"); // add at index 1fruits.remove("banana"); // remove by value
for (String f : fruits) { // traverse System.out.println(f);}
System.out.println(fruits.size()); // 2Building a seating grid one row at a time:
seats = []
for row in range(3): # add three rows seats.append([False] * 4) # each row has four free seats
seats[0][2] = True # book row 0, seat 2
for row in seats: # traverse the whole grid print(row)
# [False, False, True, False]# [False, False, False, False]# [False, False, False, False]| Task | Array (Java) | List (Python list / Java ArrayList) |
|---|---|---|
| Create it | int[] a = new int[5]; | a = [] / new ArrayList<>() |
| Read element 2 | a[2] | a[2] / list.get(2) |
| Change element 2 | a[2] = 9; | a[2] = 9 / list.set(2, 9) |
| Add a new element | Not possible: build a longer array and copy across | a.append(x) / list.add(x) |
| Remove an element | Not possible: shift the later elements by hand | a.remove(x) / list.remove(x) |
| Count the elements | a.length | len(a) / list.size() |
| Two-dimensional form | int[][] with fixed row and column counts | list of lists, rows may differ in length |