Download presentation
Presentation is loading. Please wait.
1
CS 200 Arrays, Loops and Methods
Jim Williams, PhD
2
This Week Team Lab: Arrays and Hangman BP1 Milestone 1 due Thursday
drawing diagrams (bring paper and pencil) BP1 Milestone 1 due Thursday Lecture: More Arrays and Methods
3
Key Concepts Memory Access Drawing a picture of memory for an array
Looping through multidimensional arrays appropriate use of .length Unit Testing Common Array algorithms
4
Question 1 minute to answer
I want to keep the results to analyze later
5
What will print out? public static void methodA(int [] list) {
} public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it.
6
Memory Access Local variables and parameters (stored on stack) are only visible within the method itself. to share value, return value from method, caller captures and uses Instances/objects (stored on heap) are available anywhere using the reference. Follow the reference from inside or outside the method to shared memory area (heap).
7
Sorting Demo 9, 6, 4, 5, 8, 2 import java.util.Arrays;
public class BubbleSort { public static void swapInts(int a, int b) { int temp = a; a = b; b = temp; } public static void main(String[] args) { int x = 1; int y = 2; System.out.println("x = " + x + ", y = " + y); swapInts(x, y); int[] list = new int[]{10, 5, 3, 7, 9}; printArray(list); swapInts(list[0], list[4]); swapElements(list, 0, 4); bubbleSort(list); addElementAtEnd(list, 12); list = addElementAtEnd(list, 4); public static void printArray( int [] arr) { System.out.println( Arrays.toString( arr)); public static void swapElements(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length -1; j++) { if (arr[j] > arr[j+1]) { swapElements(arr, j, j+1); public static int[] addElementAtEnd( int[] arr, int elem) { int newLength = arr.length + 1; int[] newArray = new int[newLength]; newArray[i] = arr[i]; newArray[newArray.length - 1] = elem; return newArray;
8
4 Questions First 3, 1 minute each Last 1, 2 minutes (sorting)
9
What will print out? public static void methodA(int [] list) {
list = new int[3]; list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference changed prior to accessing array contents. Changes to array contents are only visible within the method since the new array is not returned.
10
What will print out? public static void methodA(int [] list) {
list = new int[3]; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed, prior to changing array reference. Change in array visible outside the method
11
What will print out? public static void methodA(int [] list) {
} public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed. Change visible anywhere reference to array is accessed since array contents are on the heap.
12
Add Element At Front of Array
TopHat Reorder the lines
13
Draw a picture int [] items = new int []{5,6,7};
14
What does this do? int [] list = new int[4];
for ( int i = 0; i <= list.length; i++) { list[i] = i * 2; } set each element to i * 2 0, 2, 4, 6 compiler error runtime error try it.
15
Is it possible for methodC to change the contents of the array?
public static void main(String []args) { char [] items = new char[]{'c','b','a'}; methodC( items); }
16
Where in memory is variable z?
public static void main(String []args) { int r = 3; int c = 4; int [][] z = new int[r][c]; } stack heap it is local Error A and C are both correct. Local variables are those declared within methods and are stored in the stack.
17
Does createList return a new array?
public static int [] createList(int size) { return new int[size]; } public static void main(String[]args) { int [] list = createList( 10); list[3] = 10; yes no reference lost Error try it.
18
Can main see the changed value within list?
public static void changeList(int [] list) { if ( list.length > 0) { list[0] = 10; } public static void main(String []args) { int [] z = new int[5]; changeList( z); System.out.println( z[0]); yes no reference lost Error try it.
19
Can you call this method to get the new array?
public static void createList(int [] list) { list = new int[10]; } yes no reference lost Error try it.
20
What prints out? num:10 list:[1, 2, 11] num:11 list:[1, 2, 3]
static void change( int num, int [] list) { num = 11; list[2] = num; } public static void main( String [] args) { int [] list = {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); num:10 list:[1, 2, 11] num:11 list:[1, 2, 3] list:[1, 2, 10] try it.
21
What prints out? num:10 list:[1, 2, 11] list:[4,5,10] list:[1, 2, 3]
static void change( int num, int [] list) { list = new int[]{4,5,6}; list[2] = num; } public static void main( String [] args) { int [] list = new int[] {1,2,3}; int num = 10; change( num, list); System.out.println("num:" + num + "\nlist:" + Arrays.toString( list)); num:10 list:[1, 2, 11] list:[4,5,10] list:[1, 2, 3] list:[1, 2, 10] try it.
22
BP1 Notes Process: 3 milestones then further testing
Concepts: Use only material from Chapters 1-8 Teams: Once you submit as a team for Milestone 1 then you are a team for the entire project. If your team is not working you must dismantle your team by notifying an instructor by by 11:59pm on Milestone 1 (tonight). Then you work alone for the project.
23
Draw a Picture int [][] board; board = new int[3][2];
How do you find the number of rows? How do you find the number of columns?
24
Draw a Picture int [][] board; board = new int[][]{{1,2},{4,5,6}};
25
Memory Access Local variables and parameters (stored on stack) are only visible within the method itself. to share value, return value from method, caller captures and uses Instances/objects (stored on heap) are available anywhere using the reference. Follow the reference from inside or outside the method to shared memory area (heap).
26
Can you call this method to get the new array outside the method?
static void createBoard(int [][] board) { board = new int[3][3]; } yes no sometimes Error try it.
27
Can you call this method to get the new array?
static int [][] createBoard(int r, int c) { return new int[r][c]; } yes no reference lost Error try it.
28
What will print out? public static void changeList(int [][] list) {
list[0] = new int[3]; list[0][1] = 9; } public static void main(String []args) { int [][] z = new int[5][5]; changeList( z); System.out.println( z[0][1]); 3 5 9 try it.
29
Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] board) { for ( int i = 0; i < 2; i++) for ( int j = 0; j < 3; j++) board[ i ][ j ] = 1; try it.
30
Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] b) { for ( int i = 0; i < b[i].length; i++) for ( int j = 0; j < b[j].length; j++) b[ i ][ j ] = 1; try it.
31
Will this initialize the array to all 1's?
yes no don't know Error public static void main(String []args) { int [][] board = {{2,3},{4,3,2}}; initBoard( board); } static void initBoard(int[][] b) { for ( int i = 0; i < b.length; i++) for ( int j = 0; j < b[ i ].length; j++) b[ i ][ j ] = 1; try it.
32
What will print out? public static void methodA(int [] list) {
list = new int[3]; list[0] = 9; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference changed prior to accessing array contents. Changes to array contents are only visible within the method since the new array is not returned.
33
What will print out? public static void methodA(int [] list) {
list = new int[3]; } public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed, prior to changing array reference. Change in array visible outside the method
34
What will print out? public static void methodA(int [] list) {
} public static void main(String []args) { int [] list = new int[]{1,2,3}; methodA( list); System.out.println( list[0]); 1 9 list[0] try it. In method, array reference followed and contents changed. Change visible anywhere reference to array is accessed since array contents are on the heap.
35
Unit Testing (method) Developer Writes method to meet specification
Tester Verifies that method meets specification
36
Specification /** * This method returns the number of 9s in the table.
table a 2 dimensional array of int the number of 9s in table */ public static int num9s(int [][]table) { return -1; }
37
Common Algorithms for Arrays
Searching Sorting Palindrome?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.