int[] highScores = {5,2,7,8,2,4};
1) Declaring and Creating an Array
int[] ratings;
System.out.println(ratings);
null
Arrays.toString(ratings)
null
2) Using new to Create Arrays
ratings = new int[5];
System.out.println(ratings);
[I@7d86980e
System.out.println(Arrays.toString(ratings));
[0, 0, 0, 0, 0]
String[] titles = new String[5];
System.out.println(Arrays.toString(titles));
[null, null, null, null, null]
Integer[] durations = new Integer[5]; // boxed !
System.out.println(Arrays.toString(durations));
[null, null, null, null, null]
3) Initializer Lists to Create Arrays
String[] titles = {"Robocop", "Alien", "Terminator"};
System.out.println(Arrays.toString(titles));
[Robocop, Alien, Terminator]
4) Array length
System.out.println(titles.length)
3
5) Access and Modify Array Values
System.out.println(titles[0]);
Robocop
System.out.println(titles[0].charAt(2));
b
titles[0] = "Star Wars";
Arrays.toString(titles)
[Star Wars, Alien, Terminator]
titles[1] = "Space Cowboys";
titles[2] = "Back to the Future";
Arrays.toString(titles);
[Star Wars, Space Cowboys, Back to the Future]
titles[3] = "Apollo 13";
---------------------------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at .(#51:1)
titles[-1] = "Don't Open the Door";
---------------------------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 3
at .(#54:1)
titles[1] = 666;
| titles[1] = 666;
incompatible types: int cannot be converted to java.lang.String
1) Index Variables
String[] titles = {"Robocop", "Alien", "Terminator", "Star Wars", "Back to the Future"};
for (int i = 0; i < titles.length; i++) {
System.out.println( titles[i] );
}
Robocop Alien Terminator Star Wars Back to the Future
String[] titles = {"Robocop", "Alien", "Terminator", "Star Wars", "Back to the Future"};
for (String s : titles) {
System.out.println( s );
}
Robocop Alien Terminator Star Wars Back to the Future
int[] years = {1987, 1079, 1984, 1977, 1985};
for (int i=0; i < years.length; i++) {
years[i] + 100;
}
// System.out.println( Arrays.toString(years) );
int[] years = {1987, 1079, 1984, 1977, 1985};
for (int i=0; i < years.length; i++) {
years[i] = years[i] + 1000;
}
// System.out.println( Arrays.toString(years) );
int[] years = {1987, 1079, 1984, 1977, 1985};
for (int y : years) {
y = y + 1000;
}
// System.out.println( Arrays.toString(years) );
String[] titles = {"Robocop", "Alien", "Terminator", "Star Wars", "Back to the Future"};
for (String s : titles) {
s.toUpperCase();
}
// System.out.println( Arrays.toString(titles) );
String[] titles = {"Robocop", "Alien", "Terminator", "Star Wars", "Back to the Future"};
for (String s : titles) {
s = s.toUpperCase();
}
// System.out.println( Arrays.toString(titles) );
String[] titles = {"Robocop", "Alien", "Terminator", "Star Wars", "Back to the Future"};
for (String s : titles) {
s = "BOH";
}
// System.out.println( Arrays.toString(titles) );
import java.util.Date;
Date releases[] = { new Date(87, 6, 17),
new Date(85, 4, 24),
new Date(84, 9, 25),
new Date(77, 4, 24),
new Date(85, 6, 2)
};
System.out.println( Arrays.toString(releases) + '\n');
for (int i=0; i < releases.length; i++) {
releases[i].setYear(releases[i].getYear() + 1000);
}
// System.out.println( Arrays.toString(releases) );
[Fri Jul 17 00:00:00 CEST 1987, Fri May 24 00:00:00 CEST 1985, Thu Oct 25 00:00:00 CET 1984, Tue May 24 00:00:00 CEST 1977, Tue Jul 02 00:00:00 CEST 1985]
Date releases[] = { new Date(87, 6, 17),
new Date(85, 4, 24),
new Date(84, 9, 25),
new Date(77, 4, 24),
new Date(85, 6, 2)
};
System.out.println( Arrays.toString(releases) + '\n');
for (Date d : releases) {
d.setYear(d.getYear() + 1000);
}
// System.out.println( Arrays.toString(releases) );
[Fri Jul 17 00:00:00 CEST 1987, Fri May 24 00:00:00 CEST 1985, Thu Oct 25 00:00:00 CET 1984, Tue May 24 00:00:00 CEST 1977, Tue Jul 02 00:00:00 CEST 1985]
while
?¶Date releases[] = { new Date(87, 6, 17),
new Date(85, 4, 24),
new Date(84, 9, 25),
new Date(77, 4, 24),
new Date(85, 6, 2)
};
int i=0;
while (i < releases.length) {
Date d = releases[i];
d.setYear(d.getYear() + 1000);
i++;
}
// System.out.println( Arrays.toString(releases) );
We want an array with 5 dates having consecutive years
Date releases[] = new Date[5];
Date d = new Date();
for (int i = 0; i < releases.length; i++){
d.setYear(80 + i);
d.setMonth(5);
d.setDate(10); // sets the day
releases[i] = d;
}
// System.out.println( Arrays.toString(releases) );
A knight explores a cavern in search of rare objects. As soon as he finds what he's looking for, he rejoices and goes back. We represent the dungeon
as an array of strings. The variable goal
holds the object to find. Write some code to PRINT the output.
break
!explore("the treasure",
{"a rock","a trap","some swords", "the treasure","a spiderweb", "a bolete of the tombs"})
Entering..
Found a rock
Found a trap
Found some swords
How lucky! I found a treasure.
Going back!
Found some swords
Found a trap
Found a rock
Exiting..
explore("the talisman of power", {"a trap","a bolete of the tombs","a spiderweb"});
Entering..
Found a trap
Found a bolete of the tombs
Found a spiderweb
Alas, there is no treasure!
Going back
Found a spiderweb
Found a bolete of the tombs
Found a trap
Exiting..
String goal = "the treasure";
String[] dungeon = {"a rock","a trap","some swords","the treasure","a spiderweb", "a bolete of the tombs"};
// goal = "the talisman of power";
// String[] dungeon = {"a trap","a bolete of the tombs","a spiderweb"};
// write here
import java.util.ArrayList;
ArrayList<String> videogames = new ArrayList();
videogames.add("Prince of Persia");
videogames.add("Legend of Zelda");
videogames.add("Monkey Island");
videogames.add("Doom");
System.out.println(videogames);
[Prince of Persia, Legend of Zelda, Monkey Island, Doom]
videogames.remove(3); // removing from right end is fast
Doom
System.out.println(videogames);
[Prince of Persia, Legend of Zelda, Monkey Island]
Internally, it's more like an array
videogames.get(1)
Legend of Zelda
videogames.set(1, "Bubble bubble"); // returns the removed element
Legend of Zelda
System.out.println(videogames);
[Prince of Persia, Bubble bubble, Monkey Island]
videogames.size(); // 3
videogames.add("Street Fighter", 1)` // inserts at arbitrary position, slow!
videogames.contains("Monkey Island");
true
videogames.indexOf("Monkey Island");
2
videogames.indexOf("Gioco del pippero");
-1
videogames.remove("Monkey Island"); // only first occurrence !
true
System.out.println(videogames);
[Prince of Persia, Bubble bubble]
videogames.remove("Monkey Island");
videogames.remove(2);
videogames.remove(videogames.indexOf("Monkey Island"));
if (videogames.contains("Monkey Island")){
videogames.remove("Monkey Island");
}
int i = videogames.indexOf("Monkey Island");
if (i != -1) {
videogames.remove(i);
}
import java.util.ArrayList;
ArrayList<String> videogames = new ArrayList<String>();
videogames.add("Prince of Persia");
videogames.add("Monkey Island");
videogames.add("Legend of Zelda");
videogames.add("Monkey Island");
videogames.add("Doom");
System.out.println(videogames);
[Prince of Persia, Monkey Island, Legend of Zelda, Monkey Island, Doom]
videogames.remove("Monkey Island");
videogames.remove("Monkey Island");
videogames.remove("Monkey Island");
System.out.println(videogames);
videogames.remove("Monkey Island"); // removes first occurrence
videogames.remove("Monkey Island"); // removes second
videogames.remove("Monkey Island"); // no third occurrence, doesn't explode
System.out.println(videogames);
[Prince of Persia, Legend of Zelda, Doom]
7.3. Traversing ArrayLists with Loops
for (String s : videogames){
if ("Monkey Island".equals(s)){
videogames.remove(s);
}
}
Never remove nor add stuff to a sequence you are iterating with a for each
---------------------------------------------------------------------------
java.util.ConcurrentModificationException: null
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)
at .(#187:1)
import java.util.ArrayList;
ArrayList<String> videogames = new ArrayList<String>();
videogames.add("Prince of Persia");
videogames.add("Monkey Island");
videogames.add("Legend of Zelda");
videogames.add("Monkey Island");
videogames.add("Doom");
int i = videogames.indexOf("Monkey Island");
while (i != -1) {
videogames.remove(i);
i = videogames.indexOf("Monkey Island");
}
System.out.println(videogames);
[Prince of Persia, Legend of Zelda, Doom]
videogames.removeAll( Collections.singleton("MonkeyIsland") );
What happens when you allow people to write on stuff you own?
import java.util.ArrayList;
ArrayList<String> videogames = new ArrayList();
videogames.add("Prince of Persia");
videogames.add("Legend of Zelda");
videogames.add("Monkey Island");
true
List<String> myView = Collections.unmodifiableList(videogames);
myView.get(1);
Legend of Zelda
myView.add("Pong");
---------------------------------------------------------------------------
java.lang.UnsupportedOperationException: null
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1062)
at .(#282:1)
Beware you can still change original array!!!
videogames.add("Diablo");
true
myView
[Prince of Persia, Legend of Zelda, Monkey Island, Diablo]
// Java 9
List<String> computers = List.of("Apple II", "Commodore 64", "Amiga 500", "486");
computers.getClass();
class java.util.ImmutableCollections$ListN
computers.get(1);
Commodore 64
computers.set(1, "Vic 20");
---------------------------------------------------------------------------
java.lang.UnsupportedOperationException: null
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:127)
at java.base/java.util.ImmutableCollections$AbstractImmutableList.set(ImmutableCollections.java:160)
at .(#252:1)
ImmutableCollections
import java.util.ArrayList;
ArrayList<String> videogames = new ArrayList();
videogames.add("Prince of Persia");
videogames.add("Legend of Zelda");
videogames.add("Monkey Island");
// ugly new String[0] tells java the type ..
List<String> imm = List.of(videogames.toArray(new String[0]));
imm.add("Pong");
---------------------------------------------------------------------------
java.lang.UnsupportedOperationException: null
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:127)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:131)
at .(#331:1)
videogames.add("Diablo II");
System.out.println(videogames); // changed
[Prince of Persia, Legend of Zelda, Monkey Island, Diablo II]
System.out.println(imm); // no change
[Prince of Persia, Legend of Zelda, Monkey Island]
import java.util.Date;
List<String> computers = List.of("Apple II", "Commodore 64", "Amiga 500");
List<Date> releases = List.of(new Date(77,5,1), new Date(82,07,1), new Date(87,03,1));
System.out.println(releases);
[Wed Jun 01 00:00:00 CEST 1977, Sun Aug 01 00:00:00 CEST 1982, Wed Apr 01 00:00:00 CEST 1987]
releases.get(0).setYear(3077);
System.out.println(releases);
[Sun Jun 01 00:00:00 CEST 4977, Sun Aug 01 00:00:00 CEST 1982, Wed Apr 01 00:00:00 CEST 1987]
import java.time.LocalDate; // java 8, good stuff
List<LocalDate> releases = List.of(LocalDate.of(1977, 6, 1),
LocalDate.of(1982, 8, 1),
LocalDate.of(1987, 4, 1));
//releases.get(0).setYear(3077) // impossible !
System.out.println(releases);
[1977-06-01, 1982-08-01, 1987-04-01]
Suppose you visited the attic and found a stack of books, which we represent as a list of strings. Each string is prefixed by a label of one character indicating the category (D
for Detective story, T
for Travel, H
for History)
Since we are passionate about travel books, we want to examine stack one book at a time to transfer books into another pile we call ŧravel, which at the beginning is empty. We start from the top book in stack, and transfer into travel only the books starting with the label T
like ("T-Australia"
)
The non-travel books are not interesting and must be discarded.
public static void travelbook(){
ArrayList<String> stack = new ArrayList();
stack.add("H-Middle Ages"); // <---- bottom
stack.add("T-Australia");
stack.add("T-Scotland");
stack.add("D-Suspects");
stack.add("T-Caribbean"); // <---- top
ArrayList<String> travel = new ArrayList();
}
Write some code that produces the following print:
At the beginning:
stack: ["H-Middle Ages", "T-Australia", "T-Scotland", "D-Suspects", "T-Caribbean"]
travel: []
Taken T-Caribbean
stack: ["H-Middle Ages", "T-Australia", "T-Scotland", "D-Suspects"]
travel: ["T-Caribbean"]
Discarded D-Suspects
stack: ["H-Middle Ages", "T-Australia", "T-Scotland"]
travel: ["T-Caribbean"]
Taken T-Scotland
stack: ["H-Middle Ages", "T-Australia"]
travel: ["T-Caribbean", "T-Scotland"]
Taken T-Australia
stack: ["H-Middle Ages"]
travel: ["T-Caribbean", "T-Scotland", "T-Australia"]
Discarded H-Middle Ages
stack: []
travel: ["T-Caribbean", "T-Scotland", "T-Australia"]
There are two stacks of objects right_stack
and left_stack
which we represent as lists of strings. A cowboy shoots the objects at the top of the stacks, alternating the stack at each shoot. The cowboy always hits the target, so each shot decreases a stack.
"R"
or "L"
characterExample - given:
public static void bang(){
ArrayList<String> left_stack = new ArrayList();
left_stack.add("box");
left_stack.add("boot");
left_stack.add("horseshoe");
left_stack.add("bucket");
ArrayList<String> right_stack = new ArrayList();
left_stack.add("bin");
left_stack.add("saddle");
left_stack.add("tin can");
String shoot = "R";
// write here
}
after your code, it must print:
Ready?
left_stack: ["box", "boot", "horseshoe", "bucket"]
right_stack: ["bin", "saddle", "tin can"]
BANG! right: tin can
left_stack: ["box", "boot", "horseshoe", "bucket"]
right_stack: ["bin", "saddle"]
BANG! left: bucket
left_stack: ["box", "boot", "horseshoe"]
right_stack: ["bin", "saddle"]
BANG! right: saddle
left_stack: ["box", "boot", "horseshoe"]
right_stack: ["bin"]
BANG! left: horseshoe
left_stack: ["box", "boot"]
right_stack: ["bin"]
BANG! right: bin
left_stack: ["box", "boot"]
right_stack: []
BANG! left: boot
left_stack: ["box"]
right_stack: []
Nothing to shoot on the right!
left_stack: ["box"]
right_stack: []
BANG! left: box
left_stack: []
right_stack: []
1) Free Response - String Scramble B
2) Free Response - Climbing Club A
3) Free Response - Climbing Club B
4) Free Response - Climbing Club C
5) Free Response - CookieOrder A
6) Free Response - CookieOrder B
7) Free Response - StringFormatter A
8) Free Response - StringFormatter B
9) Free Response - Delimiters A
10) Free Response - Delimiters B
11) Free Response - Grid World A (Not Complete)
1) Sequential Search
2) Binary Search
3) Runtimes
4) Programming Challenge : Search Runtimes
5) Summary
1) Selection Sort no 2) Insertion Sort no 3) Programming Challenge : Sort Runtimes no
1) POGIL Groupwork: Data Privacy
2) Summary