Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Java Data structures
- // different approaches of storing, accessing and bending data to your will
- //Recursion
- // https://www.udemy.com/java-tutorial/learn/#/lecture/320992
- public class Application {
- public static void main(String[] args) {
- // Ex: 5! (5 x 4 x 3 x 2 x 1);
- System.out.println(factorial(5));
- }
- private static int factorial(int value) {
- System.out.println(value);
- // If the value is 1, stop and just return 1.
- if (value == 1) {
- return 1;
- }
- // if the value is > 1, we will subtract it by 1 and multiple it by
- // itself.
- return factorial(value - 1) * value; // (5-1) * 5
- }
- }
- //Results:
- 5
- 4
- 3
- 2
- 1
- 120
- Explanation:
- fact(5) = fact(4) * 5;
- = fact(3) * 4 * 5;
- =(fact(2) * 3) * 4;
- =((fact(1) * 2) * 3) * 4;
- =((1 * 2) * 3) * 4;
- //ArrayList print out everything in the Array (iteration)
- import java.util.ArrayList;
- public class TemperaturesC {
- public static void main(String[] args) {
- ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
- weeklyTemperatures.add(78);
- weeklyTemperatures.add(67);
- weeklyTemperatures.add(89);
- weeklyTemperatures.add(94);
- weeklyTemperatures.add(2, 111);
- for (int j = 0; j < weeklyTemperatures.size(); j++){
- System.out.println(weeklyTemperatures.get(j));
- }
- }
- }
- //Resuts:
- 78
- 67
- 111
- 89
- 94
- //For each: array
- for (Integer grade : quizGrades){
- System.out.println(grade);
- }
- // In the example above, the colon (:) can be read as "in".
- // The for each loop altogether can be read as "for each Integer element (called grade) in quizGrades, print out the value of grade."
- import java.util.ArrayList;
- public class TemperaturesForEach {
- public static void main(String[] args) {
- ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
- weeklyTemperatures.add(78);
- weeklyTemperatures.add(67);
- weeklyTemperatures.add(89);
- weeklyTemperatures.add(94);
- for (Integer temperature: weeklyTemperatures) {
- System.out.println(temperature);
- }
- }
- }
- //Hash maps: like looking through a dictionary. HashMaps.
- // Like if I was suppose to find "Max" for example. I'll go to the "M" section, etc etc
- // process is very similar to Arrays, except has <String, Integer> instead of just <Integer>.
- import java.util.HashMap;
- public class Restaurant {
- public static void main(String[] args) {
- HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
- //add items to the HashMap
- restaurantMenu.put("Turkey Burger", 13);
- restaurantMenu.put("Naan Pizza", 11);
- restaurantMenu.put("Cranberry Kale Salad", 10);
- //print out the price of the Naan Pizza
- System.out.println( restaurantMenu.get("Naan Pizza") );
- // Loop through and print out the whole menu:
- //The keySet method of HashMap returns a list of keys.
- for (String item : restaurantMenu.keySet() ) {
- System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
- }
- }
- //Results:
- 11
- 3
- A Turkey Burger costs 13 dollars.
- A Cranberry Kale Salad costs 10 dollars.
- A Naan Pizza costs 11 dollars.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement