Array Lists Chris Piech CS 106 A Stanford
- Slides: 35
Array. Lists Chris Piech CS 106 A, Stanford University Piech, CS 106 A, Stanford University
Why is this hard to write? Piech, CS 106 A, Stanford University
Why is this hard to write? Piech, CS 106 A, Stanford University
Data Structures Operation Strings Arrays Make a new one String str = “abc”; int arr = new int[5]; Get length? str. length() arr. length Get element? str. char. At(i) arr[i] Set element? Not allowed arr[i] = 5; for(int i = 0; i < str. length(); i++) for(int i = 0; i < arr. length(); i++) Loop? Piech, CS 106 A, Stanford University
Review: Reverse Array Program public void run() { int n = read. Int("Enter number of elements: "); String array. To. String(int[] array) private void int[]reverse. Array(int[] create. Index. Array(int array) n) {{ { int[] int. Array = create. Index. Array(n); String str int[] for (int array i = ==""; 0; new i < int[n]; array. length / 2; i++) { println("Forward: " + array. To. String(int. Array)); private void swap. Elements(int[] array, p 1, int p 2) { for (int i++)int { forswap. Elements(array, ( intii==0; 0; ii<<array. length; n; i, i++ array. length ) { - i - 1); reverse. Array(int. Array); int temp array[p 1]; (i > =0) } if array[i] = str i; += ", "; println("Reverse: " + array. To. String(int. Array)); array[p 1] = array[p 2]; str += array[i]; } } n int. Array } array[p 2] = temp; } return array; in array i array } return "[" + str + "]"; 10 temp p 1 p 2 array } } 5 4 3 2 1 0 10 01 9 8 7 6 5 4 3 2 str i 109 array 0 0 0, 0, 1, 3 4, 1 2, 4 5, 5 6, 6 7, 2 3, 7 8, 8 9 10 0123456789 90 180 270 603 504 540 630 720 810 09 0 1 2 3 4 5 6 7 8 9 Reverse. Array Enter number of elements: 10 Forward: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Reverse: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Piech, CS 106 A, Stanford University skip simulation
What does this say? 53‡‡† 305))6*; 4826)4‡ • )4‡); 806*; 48† 8¶ 60))85; 1‡(; : ‡*8† 83(88)5*†; 46(; 88*96* ? ; 8)*‡(; 485); 5*† 2: *‡(; 4956*2(5*– 4)8¶ 8*; 4069285); )6† 8)4‡‡; 1(‡ 9; 48081; 8: 8‡ 1; 48† 85; 4)485† 528806*81(‡ 9; 48; (88; 4( ‡? 34; 48)4‡; 161; : 188; ‡? ; Puzzle in Gold Bug by Edgar Allan Poe Piech, CS 106 A, Stanford University
Cryptogram • A cryptogram is a puzzle in which a message is encoded by replacing each letter in the original text with some other letter. The substitution pattern remains the same throughout the message. Your job in solving a cryptogram is to figure out this correspondence. • One of the most famous cryptograms was written by Edgar Allan Poe in his short story “The Gold Bug. ” • In this story, Poe describes the technique of assuming that the most common letters in the coded message correspond to the most common letters in English, which are E, T, A, O, I, N, S, H, R, D, L, and U. Edgar Allan Poe (1809 -1849) Piech, CS 106 A, Stanford University
Letter Frequency By Peter Norvig Head of Google Research Former CS 221 Instructor Based on 3. 8 trillion letters Piech, CS 106 A, Stanford University
Poe’s Cryptographic Puzzle 53‡‡† 305))6*; 4826)4‡ • )4‡); 806*; 48† 8¶ 60))85; 1‡(; : ‡*8† 83(88)5*†; 46(; 88*96* ? ; 8)*‡(; 485); 5*† 2: *‡(; 4956*2(5*– 4)8¶ 8*; 4069285); )6† 8)4‡‡; 1(‡ 9; 48081; 8: 8‡ 1; 48† 85; 4)485† 528806*81(‡ 9; 48; (88; 4( ‡? 34; 48)4‡; 161; : 188; ‡? ; AG 5 3 O ‡O ‡D †G 3 L 0 A 5 S )S )I 6 N *T ; H 4 E 8 B 2 I 6 S )H 4 O ‡P • S )H 4 O ‡S )T ; E 8 L 0 I 6 N *T ; H 4 E 8 D †E 8 V ¶ 6 L I 0 S )S )E 8 A 5 T ; F 1 O ‡R (T ; Y : O ‡N *E 8 D †E 8 G 3 R (E 8 E 8 S )A 5 N *D †T ; H 4 I 6 R (T ; E 8 E 8 N *M 9 I 6 N * ? T U ; E 8 S )N *O ‡R (T ; H 4 E 8 A 5 S )T ; A 5 N *D †B 2 Y : N *O ‡R (T ; H 4 M 9 A 5 I 6 N *B 2 R (A 5 N *C –H 4 S )E 8 V ¶ 8 N E *T ; H 4 L 0 I 6 M 9 B 2 E 8 A 5 S )T ; S )I 6 D †E 8 S )H 4 O ‡O ‡T ; F 1 R (O ‡M 9 T ; H 4 E 8 L 0 E 8 F 1 T ; E 8 Y : E 8 O ‡ 1 T F ; H 4 E 8 D †E 8 A 5 T ; H 4 S )H 4 E 8 A 5 D †A 5 B 2 E 8 E 8 L 0 I 6 N *E 8 F 1 R (O ‡M 9 T ; H 4 E 8 T ; R (E 8 E 8 T ; H 4 R ( ‡U O ? G 3 H 4 T ; H 4 E 8 S )H 4 O ‡T ; F 1 I 6 F 1 T ; Y : F 1 E 8 E 8 T ; O ‡U ? T ; Piech, CS 106 A, Stanford University 8 ; 4 ‡ ) * 5 6 ( † 1 0 9 2 : 3 ? ¶ – • 33 26 19 16 16 13 12 11 10 8 8 6 5 5 4 4 3 2 1 1
Implementation Strategy The basic idea behind the program to count letter frequencies is to use an array with 26 elements to keep track of how many times each letter appears. As the program reads the text, it increments the array element that corresponds to each letter. T W A S B RI L L I G 0 1 1 0 0 0 10 0 2 0 0 0 1 1 0 0 0 1 2 0 10 10 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Piech, CS 106 A, Stanford University
Putting Data In An Array To the code!
Putting Data In An Array Imagine a better world…. Piech, CS 106 A, Stanford University
Array. Lists This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2. 5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. Piech, CS 106 A, Stanford University
Array. List An ordered, resizable list of information Homogeneous Can add and remove elements (among other cool functionality) Can store any object type Requires importing java. util. *; Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List Type of thing your Array. List will store. Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List Same type here, but followed by (). Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); // Access elements by index (starting at 0!) println(my. Array. List. get(0)); // prints “hi” println(my. Array. List. get(1)); // prints “there” Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); // Access elements by index (starting at 0!) println(my. Array. List. get(0)); // prints “hi” println(my. Array. List. get(1)); // prints “there” // Wrong type - bad times! Won’t compile GLabel label = new GLabel(“hi there”); my. Array. List. add(label); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); // Access elements by index (starting at 0!) println(my. Array. List. get(0)); // prints “hi” println(my. Array. List. get(1)); // prints “there” // Wrong type - bad times! Won’t compile GLabel label = new GLabel(“hi there”); my. Array. List. add(label); // Invalid index – crashes! Index. Out. Of. Bounds Exception println(my. Array. List. get(2)); Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); // Access elements by index (starting at 0) for (int i = 0; i < my. Array. List. size(); i++) { String str = my. Array. List. get(i); println(str); } // hi // there Piech, CS 106 A, Stanford University
Our First Array. List<String> my. Array. List = new Array. List<String>(); // Adds elements to the back my. Array. List. add(“hi”); my. Array. List. add(“there”); // Access elements by index (starting at 0) for (int i = 0; i < my. Array. List. size(); i++) { String str = my. Array. List. get(i); println(str); } // Beautiful way to access each element for (String str : my. Array. List) { println(str); } Piech, CS 106 A, Stanford University
Methods in the Array. List Class boolean add(<T> element) Adds a new element to the end of the Array. List; the return value is always true. void add(int index, <T> element) Inserts a new element into the Array. List before the position specified by index. <T> remove(int index) Removes the element at the specified position and returns that value. boolean remove(<T> element) Removes the first instance of element, if it appears; returns true if a match is found. void clear() Removes all elements from the Array. List. int size() Returns the number of elements in the Array. List. <T> get(int index) Returns the object at the specified index. <T> set(int index, <T> value) Sets the element at the specified index to the new value and returns the old value. int index. Of(<T> value) Returns the index of the first occurrence of the specified value, or -1 if it does not appear. boolean contains(<T> value) Returns true if the Array. List contains the specified value. boolean is. Empty() Returns true if the Array. List contains no elements. Using portions of slides by Eric Roberts Piech, CS 106 A, Stanford University
Array. Lists + Primitives = �� // Doesn’t compile Array. List<int> my. Array. List = new Array. List<int>(); Array. Lists can only store objects! Piech, CS 106 A, Stanford University
Array. Lists + Primitives = �� Primitive “Wrapper” Class int double boolean char Integer Double Boolean Character Piech, CS 106 A, Stanford University
Array. Lists + Wrappers = ❤�� // Just use wrapper class when making an Array. List<Integer> num. List = new Array. List<Integer>(); num. List. add(123); num. List. add(546); int first. Num = num. List. get(0); // 123 int second. Num = num. List. get(1); // 456 Conversion happens automatically! Piech, CS 106 A, Stanford University
lets see a simple example. This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2. 5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. Piech, CS 106 A, Stanford University
Data Structures Operation Arrays Array. Listss Make a new one int arr = new int[5]; Array. List<String> list = new Array. List<String>(); arr. length list. size(); Get element? arr[i] list. get(i); Set element? arr[i] = 5; list. set(i, value); list. add(value); for(int i = 0; i < arr. length(); i++) for(String value : list) Get length? Loop? Piech, CS 106 A, Stanford University
Winter is over Thanks Keith Schwarz for some great slides to build off! Piech, CS 106 A, Stanford University This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2. 5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.
- Chris piech
- Chris piech stanford
- Karolina piech
- Karolina piech
- Photovoltaic array maximum power point tracking array
- Char b 6 6 6 dengan alamat awal
- Array of array in c
- Jagged array
- Upper triangular array adalah
- Associative array vs indexed array
- Contoh program array 1 dimensi python
- Pin grid array vs land grid array
- Difference between broadside array and endfire array
- Roberts building 106
- 100 101 102
- Section 106 compliance
- Cs 106 a
- Soc 106
- Iat 106
- Metabolismo glucidico
- Tc 106
- 106 pal
- Col 106 amit kumar
- Nur 106
- Physics 106
- Numbers 201 to 250
- Salmo 106
- Zodiac telemetry
- 106 spelled out
- Col 106
- Iat 106
- Col 106
- Round the factors and estimate the products 656 x 106
- Iat 106
- 106 signal brigade
- 105106