Advertisement
yo2man

Java DataStructures, HashMaps, Arrays, Cheat Sheet

Mar 15th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. // Java Data structures
  2. // different approaches of storing, accessing and bending data to your will
  3.  
  4. //Recursion
  5. // https://www.udemy.com/java-tutorial/learn/#/lecture/320992
  6.  
  7. public class Application {
  8.     public static void main(String[] args) {
  9.  
  10.         // Ex: 5! (5 x 4 x 3 x 2 x 1);
  11.         System.out.println(factorial(5));
  12.     }
  13.  
  14.     private static int factorial(int value) {
  15.         System.out.println(value);
  16.  
  17.         // If the value is 1, stop and just return 1.
  18.         if (value == 1) {
  19.             return 1;
  20.         }
  21.  
  22.         // if the value is > 1, we will subtract it by 1 and multiple it by
  23.         // itself.
  24.         return factorial(value - 1) * value; // (5-1) * 5
  25.  
  26.     }
  27. }
  28.    
  29.    
  30.  //Results:
  31.  5
  32.  4
  33.  3
  34.  2
  35.  1
  36.  120
  37.  
  38.  
  39. Explanation:
  40. fact(5) = fact(4) * 5;
  41. = fact(3) * 4 * 5;
  42. =(fact(2) * 3) * 4;
  43. =((fact(1) * 2) * 3) * 4;
  44. =((1 * 2) * 3) * 4;
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. //ArrayList print out everything in the Array (iteration)
  55.  
  56. import java.util.ArrayList;
  57.  
  58. public class TemperaturesC {
  59.    
  60.     public static void main(String[] args) {
  61.  
  62.         ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
  63.         weeklyTemperatures.add(78);
  64.         weeklyTemperatures.add(67);
  65.         weeklyTemperatures.add(89);
  66.         weeklyTemperatures.add(94);
  67.         weeklyTemperatures.add(2, 111);
  68.  
  69.     for (int j = 0; j < weeklyTemperatures.size(); j++){
  70.     System.out.println(weeklyTemperatures.get(j));
  71.   }
  72.     }
  73. }
  74.  
  75. //Resuts:
  76. 78
  77. 67
  78. 111
  79. 89
  80. 94
  81.  
  82.  
  83. //For each: array
  84. for (Integer grade : quizGrades){
  85.     System.out.println(grade);
  86. }
  87.  
  88. // In the example above, the colon (:) can be read as "in".
  89. // The for each loop altogether can be read as "for each Integer element (called grade) in quizGrades, print out the value of grade."
  90.  
  91. import java.util.ArrayList;
  92.  
  93. public class TemperaturesForEach {
  94.     public static void main(String[] args) {
  95.  
  96.         ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
  97.         weeklyTemperatures.add(78);
  98.         weeklyTemperatures.add(67);
  99.         weeklyTemperatures.add(89);
  100.         weeklyTemperatures.add(94);
  101.        
  102.         for (Integer temperature: weeklyTemperatures) {
  103.             System.out.println(temperature);
  104.         }
  105.    
  106.     }
  107. }
  108.  
  109.  
  110. //Hash maps: like looking through a dictionary. HashMaps.
  111. // Like if I was suppose to find "Max" for example. I'll go to the "M" section, etc etc
  112. // process is very similar to Arrays, except has <String, Integer> instead of just <Integer>.
  113.  
  114. import java.util.HashMap;
  115.  
  116. public class Restaurant {
  117.     public static void main(String[] args) {
  118.  HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
  119.    
  120.      //add items to the HashMap
  121.    restaurantMenu.put("Turkey Burger", 13);
  122.    restaurantMenu.put("Naan Pizza", 11);
  123.    restaurantMenu.put("Cranberry Kale Salad", 10);
  124.    
  125.     //print out the price of the Naan Pizza
  126.     System.out.println( restaurantMenu.get("Naan Pizza") );
  127.    
  128.    
  129.     // Loop through and print out the whole menu:
  130.     //The keySet method of HashMap returns a list of keys.
  131.     for (String item : restaurantMenu.keySet() ) {
  132.         System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
  133.    
  134.     }
  135. }
  136.  
  137.  
  138.   //Results:
  139.   11
  140.   3
  141.   A Turkey Burger costs 13 dollars.
  142.   A Cranberry Kale Salad costs 10 dollars.
  143.   A Naan Pizza costs 11 dollars.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement