-
Notifications
You must be signed in to change notification settings - Fork 94
Finished Derusting project #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5a2a97f
cb2f8b6
9d21a5e
4c49d73
2de2f75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,45 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class ListPractice { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // Create an empty ArrayList of Strings and assign it to a variable of type List | ||
|
|
||
| List<String> list = new ArrayList<>(); | ||
| // Add 3 elements to the list (OK to do one-by-one) | ||
|
|
||
| list.add("Beans"); | ||
| list.add("Pineapple"); | ||
| list.add("onion"); | ||
| // Print the element at index 1 | ||
|
|
||
| list.get(1); | ||
| System.out.println(list.get(1)); | ||
| // Replace the element at index 1 with a new value | ||
| // (Do not insert a new value. The length of the list should not change) | ||
|
|
||
| list.set(1, "Orange"); | ||
| // Insert a new element at index 0 (the length of the list will change) | ||
|
|
||
| list.add(0, "apple"); | ||
| // Check whether the list contains a certain string | ||
|
|
||
| if(list.contains("onion")) { | ||
| System.out.println("Found"); | ||
| } else { | ||
| System.out.println("Not found"); | ||
| } | ||
| // Iterate over the list using a traditional for-loop. | ||
| // Print each index and value on a separate line | ||
| for (int i = 0; i < list.size(); i++){ | ||
| System.out.println("Index: " + i + ", Value: " + list.get(i)); | ||
| } | ||
|
|
||
| // Sort the list using the Collections library | ||
|
|
||
| Collections.sort(list); | ||
| // Iterate over the list using a for-each loop | ||
| // Print each value on a second line | ||
|
|
||
| for (String i : list){ | ||
| System.out.println(); | ||
| System.out.println(i); | ||
| } | ||
|
Comment on lines
+39
to
+42
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and throughout your code try to be a bit more consistent in you indentation. |
||
| /* | ||
| * Usage tip! | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,37 @@ | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| public class MapPractice { | ||
| public static void main(String[] args) { | ||
| // Create a HashMap with String keys and Integer values and | ||
| // assign it to a variable of type Map | ||
|
|
||
| HashMap<String, Integer> people = new HashMap<String, Integer>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types (Map) |
||
| // Put 3 different key/value pairs in the Map | ||
| // (it's OK to do this one-by-one) | ||
|
|
||
| people.put("John", 52); | ||
| people.put("Sarah", 43); | ||
| people.put("Charles", 23); | ||
| // Get the value associated with a given key in the Map | ||
|
|
||
| people.get("Charles"); | ||
| // Find the size (number of key/value pairs) of the Map | ||
|
|
||
| people.size(); | ||
| // Replace the value associated with a given key (the size of the Map shoukld not change) | ||
|
|
||
| people.replace("john", 56); | ||
| // Check whether the Map contains a given key | ||
|
|
||
| people.containsKey("sarah"); | ||
| // Check whether the Map contains a given value | ||
|
|
||
| people.containsValue("43"); | ||
| // Iterate over the keys of the Map, printing each key | ||
|
|
||
| for (String key : people.keySet()){ | ||
| System.out.println(key); | ||
| } | ||
| // Iterate over the values of the map, printing each value | ||
|
|
||
| for (Integer value: people.values()) { | ||
| System.out.println(value); | ||
| } | ||
| // Iterate over the entries in the map, printing each key and value | ||
|
|
||
| for (String i : people.keySet()) { | ||
| System.out.println("key: " + i + " value: " + people.get(i)); | ||
| } | ||
|
Comment on lines
+32
to
+34
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works! Can you find another method that works directly with the map entries, looping over the keys and values in a single set? |
||
| /* | ||
| * Usage tip! | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,25 @@ | ||
| import java.util.HashSet; | ||
|
|
||
| public class SetPractice { | ||
| public static void main(String[] args) { | ||
| // Create a HashSet of Strings and assign it to a variable of type Set | ||
|
|
||
| HashSet<String> helicopters = new HashSet<String>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types (Set) |
||
| // Add 3 elements to the set | ||
| // (It's OK to do it one-by-one) | ||
|
|
||
| helicopters.add("Huey"); | ||
| helicopters.add("Mi24"); | ||
| helicopters.add("cobra"); | ||
| // Check whether the Set contains a given String | ||
|
|
||
| System.out.println("Does the set contain 'Mi24' ?" + helicopters.contains("Mi24")); | ||
| // Remove an element from the Set | ||
|
|
||
| helicopters.remove("Huey"); | ||
| // Get the size of the Set | ||
|
|
||
| int size = helicopters.size(); | ||
| System.out.println("The set size is: " + size); | ||
| // Iterate over the elements of the Set, printing each one on a separate line | ||
|
|
||
| for (String helicopter : helicopters){ | ||
| System.out.println(helicopter); | ||
| } | ||
| /* | ||
| * Warning! | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,48 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class StringPractice { | ||
| public static void main(String[] args) { | ||
| // Create a string with at least 5 characters and assign it to a variable | ||
|
|
||
| String txt = "Hotdogs"; | ||
| // Find the length of the string | ||
|
|
||
| System.out.println(txt.length()); | ||
| // Concatenate (add) two strings together and reassign the result | ||
|
|
||
| String toppingOne = "Stuffed"; | ||
| String toppingTwo = "Crust"; | ||
| toppingOne = toppingOne + toppingTwo; | ||
| System.out.println(toppingOne); | ||
| // Find the value of the character at index 3 | ||
|
|
||
| String findme = "Please find me here"; | ||
| System.out.println(findme.indexOf("find")); | ||
| // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) | ||
|
|
||
| String substring = "find"; | ||
| if(findme.contains(substring)) { | ||
| System.out.println(substring); | ||
| } else { | ||
| System.out.println("There are no substring in " + substring ); | ||
| } | ||
| // Iterate over the characters of the string, printing each one on a separate line | ||
|
|
||
| for (int i = 0; i < findme.length(); i++){ | ||
| char ch = findme.charAt(i); | ||
| System.out.println(ch); | ||
| } | ||
| // Create an ArrayList of Strings and assign it to a variable | ||
|
|
||
| ArrayList<String> bedroom = new ArrayList<String>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types (List) |
||
|
|
||
| // Add multiple strings to the List (OK to do one-by-one) | ||
|
|
||
| bedroom.add("bed"); | ||
| bedroom.add("table"); | ||
| bedroom.add("lamp"); | ||
| // Join all of the strings in the list together into a single string separated by commas | ||
| // Use a built-in method to achieve this instead of using a loop | ||
|
|
||
| String joinedString = String.join(",", bedroom); | ||
| System.out.println(joinedString); | ||
| // Check whether two strings are equal | ||
|
|
||
| String f1 ="football"; | ||
| String f2 = "soccerball"; | ||
| String f3 ="football"; | ||
| System.out.println(f1.equals(f2)); | ||
| System.out.println(f1.equals(f3)); | ||
| /* | ||
| * Reminder! | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates a new array. How would you go about finding the length of the existing array
arr?