Software Engineering I SENG201
Lecture 9 – Arrays and array lists March 9, 2022
Copyright By PowCoder代写 加微信 powcoder
Reminder – poll on Learn
Lecture pace:
Poll closes 13 March at 1:00pm
Previous lecture
1. Iterations with for
2. Iterations with while, do-while
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
• Simple data structure
– Sequenceofvaluesofthesametypeinonevariable
• For example
– “Variable” i holds integers 4, 5, 3 and 1 instead of one integer only
int[] i = new int[size]; instead of int i;
– “Variable” s holds “Anneli”, “Leentje”, “Yuangfang” instead of one name
String[] s = new String[size]; instead of String s;
“Yuangfang”
Example (arrays used like other variable)
public class CourseManager {
private int[] ageList = new int[10]; private int[] phoneList;
CourseManager()
phoneList = new int[10]; public void checkStudents()
// declaration + construction with 10 elements // declaration of phoneList
// construction of phoneList with 10 elements
// no need to “hardcode” size
// array of Student objects
// first index: 0; last: size of array-1 // length is variable, remains fixed
// return array (of students)
int maxStudents = 10;
String[] names = new String[maxStudents];
Student[] seng201Students; seng201Students = new Student[200];
System.out.println(names[0]); System.out.println(names.length)
public Student[] classList() {
Initialization
• Default values in array depend on array type
– Numeric:zero;Boolean:false;Objectreferences:null
• Direct element initialization
• Can declare array and initialize array to specific values
– No need for new to construct array (implicitly sets array size)
String[] sizeList = new String[3];
sizeList[2] = “medium”; // what about 0 and 1?
String[] sizeList = {“small”, “medium”, “large”};
// the same as:
String[] sizeList = new String[3]; sizeList[0] = “small”;
sizeList[1] = “medium”; sizeList[2] = “large”;
// array size is 3
Copies of arrays and array references
int[] values = {5, 7, 9}; // creates array of size 3 int[] valuesCopy = values;
valuesCopy[1] = 100;
System.out.println(values[1]); // prints 100, why?
import java.utils.Arrays; // need to import class Arrays … // provides method copyOf()
int[] values = {5, 7, 9};
int[] valuesCopy = Arrays.copyOf(values, values.length);
// Or (“manually” copy array)
int[] values = {5, 7, 9}; int[] valuesCopy = new int [3]; for(int i = 0; i < 3; i++)
valuesCopy[i] = values[i];
A familiar example
public static void main(String[] args)
• main has a String[] parameter
– args contains command line arguments if run from terminal
C:\seng201> java CommandLineArgTester –verbose somedata.txt
– Supplied via dialog in Eclipse (Run→Run Configurations) and other IDE’s
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
• Declaration, construction and access
int [][] values;
values = new int [2][4];
// the same as:
int[][] b = new int [2][4];
b [0][2] = 42;
// declaration
// construction
// declaration + construction
// accessing (b[0, 2] won’t compile)
• Direct initialization
int[][] grid = {
{25, 87, 78, 96}, {47, 14, 45, 36}, {67, 32, 25, 98},
// the same as:
int[][] grid = new grid[3][4]; grid[0][0] = 25; grid[0][1] = 87; etc.
Initialize and print 2D arrays
• Initialization – a use for nested loops
int[][] b = new int [HEIGHT][WIDTH]; for(int row = 0; row < HEIGHT; row++)
for(int col = 0; col < WIDTH; col++) b[row][col] = row * col;
• Printing – a use for nested loops
int [][] b = new int[2][4];
for(int row = 0; row < b.length; row++)
} System.out.println();
for(int col = 0; col < b[row].length; col++) System.out.print(b[row][col] + “ ”);
Higher dimensional arrays
• Higher dimensional arrays use the same approach
• Declaration, construction and access
Fast slide
int[][][] superCube;
superCube = new int[5][5][10];
// same as:
int[][][] superCube = new int[5][5][10];
superCube[2][3][7] = 42;
// declaration
// construction
// declaration + construction // accessing
• Direct initialization
int[][][] ints = {
System.out.println(ints.length); System.out.println(ints[0].length); System.out.println(ints[0][0].length);
// 2 // 3 // 2
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
Handy utilities
• java.util.Arrays has methods for array handling – See API for details
• Useful methods
– toString prints the elements of list (print each dimension separately) – sort sorts an array
– fill bulk initialization (Arrays.fill(
int[] ints = {4, 2, 1, 3}; System.out.println(ints); System.out.println(Arrays.toString(ints)); Arrays.sort(ints); System.out.println(Arrays.toString(ints));
[4, 2, 1, 3]
[1, 2, 3, 4]
Arrays.toString() – another example
int[][] grid = {
{25, 87, 78, 96}, {47, 14, 45, 36}, {67, 32, 25, 98},
}; System.out.println(Arrays.toString(grid));
int[][] grid = {
{25, 87, 78, 96},
{47, 14, 45, 36},
{67, 32, 25, 98},
for(int i = 0; i < grid.length; i++) System.out.println(Arrays.toString(grid[i]));
[25, 87, 78, 96] [47, 14, 45, 36] [67, 32, 25, 98]
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
ArrayList (in package java.util)
• Part of Java’s collection framework
– Collection: object that represents group of objects
– Types: Lists (e.g., ArrayList), sets (e.g., HashSet), maps (e.g., HashMap)
– Provide methods for common tasks, e.g., add(), set(), remove()
• Also, grow and shrink as needed
– More about collections and types of collections in the lab • ArrayList is a class
– ArrayList instead of [] to indicate list of objects
• ArrayList is generic – has type parameter
– Useful if we are not sure about the type of elements in data structure – ArrayList
import java.util.ArrayList;
ArrayList
surnames.add(“Boehm”); surnames.add(“Sommerville”); surnames.add(1, “Pressman”); System.out.println(surnames); System.out.println(surnames.size()); System.out.println(surnames.get(2)); surnames.set(1, “van Vliet”); surnames.remove(2); System.out.println(surnames); System.out.println(surnames.size());
// built-in add()
// grows as needed
// add at index, reorganizes itself // no toString()
// get size of array list
// get element at index
// set at index
// removes and resizes
[Boehm, Pressman, Sommerville]
Sommerville
[Boehm, van Vliet]
Multi-dimensional array lists – 2D
ArrayList
Second dimension
ArrayList
// add integer list to first dimension
myTwoDIntegerList.add(new ArrayList
// access second dimension
myTwoDIntegerList.get(0).add(5);
First dimension
First dimension
Second dimension
Multi-dimensional array lists – 3D
ArrayList
// add String array list to first dimension
myStringList.add(new ArrayList
myStringList.get(0).add(new String[2]);
// access third dimension
myStringList.get(0).get(0)[0] = “Hello”;
for revisited
• Enhanced form to traverse collections conveniently
ArrayList
for(Student who : classList) {
Read as: for each element in… System.out.println(who); // assumes toString() in class Student
• Works for arrays, array lists, etc.
– Loop variable values are successive elements
– No index values, but could roll own counter if needed – Notsuitableformodifyingcollections,justvisiting
int[] ints = {4, 2, 1, 3};
int iCount = 0; // not needed to traverse array for(int currentInt : ints)
System.out.println(“Element ” + iCount++ + “ = ” +currentInt);
Current element in ints
Element 0 = 4
Element 1 = 2
Element 2 = 1
Element 3 = 3
• Alternative
int[] ints = {4, 2, 1, 3};
for(int i < 0; i < ints.length; i++) {
System.out.println(“Element ” + i + “ = ” +ints[i]);
1. Introduction to arrays
2. Multi-dimensional arrays
3. Utilities for arrays
4. Array lists
Cartoon of the day
Key lesson: Java provides built-in arrays. Array lists on the other hand are class representations of arrays (i.e., come with methods, etc.) and are part of Java’s collections framework.
https://www.iconspng.com/image/64816/array-lists-comic
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com