Repository navigation
Expand file tree
/
Copy pathArraysLecture.java
More file actions
227 lines (177 loc) · 16.8 KB
/
Copy pathArraysLecture.java
File metadata and controls
227 lines (177 loc) · 16.8 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import books.Author;
import books.LibraryBook;
import java.util.Arrays;
public class ArraysLecture {
public static void main(String[] args) {
// What's the same about arrays from JavaScript and arrays in Java?
// Arrays are collections of data!
// They are 0 indexed! (to access the third element, I need to access the array at the index point of?)
// We can save arrays to variables!
// What's different?
// How we instantiate arrays. In JS: let arr = ["banana",77, {name: "Bill", job: "computer man"}];
// In Java:
int[] nums = new int[5];
// boolean[] bools = new boolean[2];
// This will instantiate an array with 5 elements;
// System.out.println(nums[0]); // What will I get from this sout?
// System.out.println(bools[1]);
// Let's set values for our array elements using a loop!
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
// System.out.println(nums[i]);
}
// We see that iterating through an array with a for loop is similar to JS.
// System.out.println(nums[3]); // What will I get from this sout?
// System.out.println(nums[99]); // What would I have gotten from this in JavaScript? Now?
int[] otherNums = { 6,7,8,9 };
// This will instantiate an array with 4 pre-defined elements.
// System.out.println(otherNums[1]); // What do is see printed from this sout?
// System.out.println(otherNums[4]); // What do is see printed from this sout?
// Let's get a bit more complicated. :)
LibraryBook garfield = new LibraryBook("Garfield Loses His Feet", "Jim Davis", 1984, "Random House");
LibraryBook hunchback = new LibraryBook("The Hunchback of Notre-Dame", "Victor Hugo", 1831, "Gosselin");
LibraryBook superfudge = new LibraryBook("Superfudge", "Judy Blume", 1980, "E. P. Dutton");
LibraryBook grapesWrath = new LibraryBook("The Grapes of Wrath", "John Steinbeck", 1939, "The Viking Press");
LibraryBook phantomOfOpera = new LibraryBook("The Phantom of the Opera", "Gaston Leroux", 1911, "Pierre Laie");
LibraryBook lesMiserables = new LibraryBook("Les Misérables", "Victor Hugo", 1862, "Librairie internationale A. Lacroix, Verboeckhoven, et Cie.");
LibraryBook[] library = new LibraryBook[5];
library[0] = garfield;
// System.out.println(library[0].getTitle()); // What do I see in this sout?
// System.out.println(library[1]); // What do I see in this sout?
library[1] = hunchback;
library[2] = superfudge;
library[3] = grapesWrath;
library[4] = phantomOfOpera;
// We have the ability to iterate through arrays similar to our for of loops in JS!
for (LibraryBook book : library) {
String title = book.getTitle();
String author = book.getAuthor();
// System.out.format("'%s' by %s%n", title, author);
}
// Let's explore the Arrays class.
// Arrays.fill
int[] allSevens = new int[7];
Arrays.fill(allSevens, 7);
for (int seven : allSevens) {
// System.out.println(seven);
}
// What'll we see after uncommenting these lines of code?
// allSevens[2] = 8;
// Arrays.fill(allSevens, 7);
// System.out.println(allSevens[2]);
// Arrays.equals
int[] yetMoreSevens = {7,7,7,7,7,7,7};
// System.out.println(allSevens);
// System.out.println(yetMoreSevens);
// System.out.println(allSevens == yetMoreSevens); // What do I see from this sout?
// System.out.println(allSevens.length);
// System.out.println(Arrays.equals(allSevens, yetMoreSevens)); // What do you think the equals method evaluates?
// Arrays.toString
// System.out.println(allSevens); // What do I see from this sout?
// System.out.println(Arrays.toString(allSevens)); // What does the Arrays.toString do?
// Arrays.sort
int[] aMess = new int[] {34, 88, 42, 17, 22, 19, 24};
Arrays.sort(aMess);
// System.out.println(Arrays.toString(aMess)); // What order would I expect to see?
// Arrays.copyOf
// library[5] = lesMiserables; // What happens when I uncomment this line?
library = Arrays.copyOf(library, library.length + 1);
// System.out.println(library.length);
library[5] = lesMiserables;
for (LibraryBook book : library) {
String title = book.getTitle();
String author = book.getAuthor();
// System.out.format("'%s' by %s%n", title, author);
}
// Two-Dimensional Arrays
// System.out.println(funnyJoke);
int[][] twoDExample = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// accessing individual elements:
// arr[num1][num2]
// arr name of the 2D array.
// num1 = The row of the 2D array, Selecting the array within the array.
// num2 = The element of the array within the array.
// How can we sout the numbers 1, 6, and 8?
// System.out.println(twoDExample[0][0]);
// System.out.println(twoDExample[1][2]);
// System.out.println(twoDExample[2][1]);
for (int[] row : twoDExample) {
System.out.println("+---+---+---+");
System.out.print("| ");
for (int n : row) {
System.out.print(n + " | ");
}
System.out.println();
}
System.out.println("+---+---+---+");
}
private static String funnyJoke = "*//////((((((((((((((((((//////////////////((((((#####((((((((((((((((((((((/(((#################################################((((########(((((////////((((((\n" +
"*//////(((((((((((((((((((/////////////////(((((((####((((((((((((((((((((((/(((##(((###########################################(((((((#((((((((((((((////((((((\n" +
"/////((((((((((((((((((((//////////////////(((((((((###((((((((((((((((//////((((((((###########################################((((((((((((((((/////(((((((((((\n" +
"((///(((((((((((((((((((((////////////////((((((((((((###((((((((((((/////////(((/(((##########################################((((((((((((((((////((((((((((((#\n" +
"((((/(((((((((((((((((((((((((((/////////((((((((((((((((((((///////* ....,/,. ..,////(,*/((##(##########################(((((((((((((((////////(((((((((((##\n" +
"((((((((((((((((((((((((((((((((/////((((((((((((((((((((((/,... .,,***,, .,*/,, ../(,, .*. *##########################((((((((((//(((((((////////(((((((((((#\n" +
"(((((((((((((((((((((((((((((((///////((((((((((((((((((((, .. ..,(*, ..,*(*..,(/,,.,.,,*#/,***(((##############((((((((((/////////((((////////(((((((((((\n" +
"(((((((/((/(//((((((((((((((((/////////((((((((((((//(/,. ....,*,..,(#/.,,/(*,.*((,,.,,/((*..,,,.*((############(((((((((((/////(((((((((((((((((((((######\n" +
"(((((((///////////(((((((((((((/////////((((((/////* ...... ..*(*.,#*.,*(,,,(((((((((**..,,.,/#//*(############(((((((((///((((((((((((((((((((((#######(\n" +
"((((((((///////////((((((((((///////////(/////////* . ...,,,*/*,.,/*,(,/(/,,##//*//,**,.*,,,//#(/*,.,,,,(#####(((((((((((((/((((((#####((((((((((((#####(((\n" +
"((((((((////////////////((((((/(((//////////////, ....... ..*/(**(/(((/(#/(,,,*,,....,,*/#%(*...,.,,,,.######(((((((((((((((((((#######(((((((((((####(((\n" +
"(((((((((//////////(((//((((((((((((//////////, ...,.......,***/***(((((##(///**(/(/((/#((#(#(((#####%%%#%#/(####((((((((((((((((((((########(((((((((#####((\n" +
"(((/(((((//////////((((((((((((((((((/////////.... ........,,*/(##(((###(#######%##############**,.,,,**/(((((#(((((((((((((((((##########(##############\n" +
"(((((((((((////////((((((((((((((((((((/////*. ...,,,,,******//*/((((((#((###########%%%%#%###(%#/*,*#(##. .((((((#((((((((((((((((##########(((((#########\n" +
"((((((####((((/////(((((((((((((((((((((///, ....,,,,,,*,******////((((((((########%%%%%&%%%%%#((*/#%#*.,*//#.,(((####(((((((((((((((##########(((((((#######\n" +
"((((((####(((((((((((####((((((((((((((////. .......,,,,,*********///(((((((###(####%%%%%&&&%&&&%%%#(**/(##(*. ,,,(#######((((((((((###############((((((#######\n" +
"((((((((((((((((((((((((#####((((((((//////*. ..,,,,,,,,,,,******////((((((#######%%%&&&&&&&&&&%%%#(/, .//(#%(. ,(#####(((((((((###############((((((#########\n" +
"((((((((((((((##(((((((((###(((((//////////, ....,,,,,,,,,,,******////////((######%%%&&%&%%&&&&&%####/((#,.,**((#* ,#######((((((################(((############\n" +
"((((((((((((((###((((((((((((((((/////////, ,. ..,,,,,,,,,,,,****/*////////((((((##%%%%%%%%%&&%&&%%%%%%#(//* .,***(#######((((((#################((############\n" +
"((((((((((((((((((((((((((((///////////////.. ..,,,,,.,,,,,,,,*****///(/(/((((//((###%#%%%%%%%%%%&%%%%%%%##%*. ..,/#######(((((((###############(((############\n" +
"(((((((####((((((((((((((((((//////////////.. ...,,.,.,,,,,,*******//((((////*//(######%#%%%%%%%%%%%%%%%%%%%#(*, ./#######(((((((##############((((###########\n" +
"((((((########(((((((((///(((((((((//////// ....,....,,,,,,,**//(((((///**///(((##%%%%&%%%%%####%#%%#%%%%%##(/*.,%%*,(#####(((((((##############((((##########\n" +
"((((((#########(((((((//////(((((((((//////. .......,,,,******////*/(((#%**//(###(//(*,*,***/(((((#####%#%%###(/**(##/*(#####((((((((##############((###########\n" +
"#(((((############((((//////((((((((((((((/ ..,,.,.,... ......,,,**/(**/(((((/*,,***,**///(#%#%####%%%%%%%(##(///%%%((######(((((((((#########(################\n" +
"#(((((##############((((///((((((((((((((((* .,,,,.,... .,.,********(/*/((/**///////((((###%%%%%&%&%%%%%%%##(((//(#%%######(((((((((#########(################\n" +
"#(((((###############(((((((((((((((((((((((/.,,,,,...,,*****//*//////*////(((/(/******,,,,**********/(%%#%%###(/##/#%###############(((###########(((##########\n" +
"##(((((####################(((((((((########(*.,,,..,,,****////*****,****/((#((/**,,.. ..,*/(#############%###((#/#%%################((#######(((((((##########\n" +
"###((((#####################((((((((((#######*,,,,,,,,,,...,,*,,...,,,,,,*/#%###(**********/(((###%%%%####%%%##(((#&&%#########################(((((((#####%%%##\n" +
"(##(((#######################((((((((((######(/,,,,... .. .,*****,,,,*/(###(((((/*///((((((###%%%%%%%%####(((((%%%##########################((((((######%%##\n" +
"(((##########(((##############((((((((((#######/,,,,,,,,,********///*,,,*//(##%%%%%%####((##(####%%&&&&&&&%####(#(/#%%##########################((((((##########\n" +
"(((#########(((((#(((##########((((((((((#######/,,,,,,,***********,,,,,*//((###%###(/***//(((###%%&&&&&&%%%###((#((###############################(((((########\n" +
"(((((########(((((((((((#########(((((((((######(,,,,,********///**,,,,*///(##%%%#((##%%(#(*,**/((##%%%%%%##((####((############################################\n" +
"(((((########((((((((((((#########(((((((((######*,,,*******///**,,*****/((###%&&&%%&&%%#####(/*,,*((####((((((##(/((###########################################\n" +
"((((((########((((((((((((##########((((((((#####(,,,******//**,,,*****////((##%##(((/(#%%%%###((/,,*/((((//(((##((/(#################################(((#######\n" +
"((((((############((((((((###########(((((((((####*,,,*******,,,,,,,,,,***//////*,,**(((((#(#####((/,,//((//(((((((/(#################################(((#######\n" +
"(((((###########((((((((((#############((((((((##(#,.,,,,***,,,,,,,..... .,,,..,**,*(/((((//***/(/,///(//(((//(//(/(########################(#####(((########\n" +
"(((((############(((((((((##############((((((((#(#(,,.,,,,,,,,,.... ......,.,,,,,,/(####%%###(/*,,***/(((///////((// .....,,/(###############(((((((((((##(####\n" +
"((((((############((((((((((##############((((((((((/.,,..,,,,... ...,,,*****///*////(////%&#,.,*****(/////(//*////( .. .........,.,*//((#(((((((((########\n" +
"((((((############((((((((((((#############((((((((((*,,,..,,,,.........,,,,,*,***((/(%%&@@@*,*/#(//,*////*/******//(#/ .... . ....................,*/((######\n" +
"(((((((###########((((((((((((((((((########((((((((((/.,,,..,.,.... /#(%&#&&@&&@@@@@@&%,*((###(((////*******,*//(((%# . ..... ... ..... ........\n" +
"(((((((############((((((((((((((((((#######((((((((((((,.,,,..,....,... */(%##///((/(//##%%%%%##(((/,/.****,///((((/%# .. .. ........ . ....\n" +
"(/(((((########(#(((((((/(//((((((#####(###(((((((((((((((....,...,,,,,..,..*/#%%%%&%(*/(#%%##%####((/*****,,*///((((((#@. . .. .. \n" +
"((((((########(((((((((((((///((((#####(((((((((((((((((((* ....,,,,,,.,,,*****/////((((((#####(//(*,,*******//(((((((&& . .. . \n" +
"(((((((########((((((((((((///(((((##(((((((((/, ....,,,,,,,,,*********/////((((((/(((,,**,****///(((((//%@. . . \n" +
"(((((((#############((((((/////(((((((((((/*, . . .,,,,,,... .....,,**//(((((((***,,,,*,,**////((((//#@* \n" +
"((((((((##############(((((//////,. ... .,,,,,,*,,,,,***//((((((/((*/,.,*,,,,*///////////*(#& \n" +
"(((///((###############((((/**. ..... .,,,,,,,****///////(////**,.,,,,******////////.,/%# \n" +
"((////(((##############(/. ,,....... .,,,,,,***/******//*,,.,,..**********/////* */%% \n" +
"(((//(((##(((#(((*. *. ......... ....,,,*,,,,..........,,,,,,,,********, (@/% \n" +
"(((/((((#((/, .(. .............. . .........,,,,,.,,,,,,,,***, (%%, \n" +
"((///(/, ,(/ ............................,,.,,,,,,,. *((&. \n" +
"(//*. ,** ........................... %%(@. \n" +
"*. *#(, ,(#(& \n" +
" .(*/ %%%#* \n" +
" ,### .##*@* \n" +
" .(&#( #%&(@, \n" +
" /#,( .@#((* \n" +
" %%#%. *%%#@ \n" +
" #*%/# &%@%@ \n" +
" *#%(# .&//%* \n" +
" ,&/%&/ (&&## \n" +
" #/&/( &((&# \n" +
" *##%%, *%%(&* \n" +
" ,&(%%( %#%(* \n" +
"YO DAWG, I HEARD YOU LIKE ARRAYS";
}