Advertisement
MrDoyle

OOP) Data Structures

Apr 9th, 2021
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3.  
  4. public class DataStructures {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         // for loop
  9.         System.out.println("For Loop:");
  10.         for (int waterLevel = 0 ; waterLevel < 7; waterLevel++){
  11.             System.out.println("The pool's water level is at " + waterLevel + " meters.");
  12.         }
  13.  
  14.         System.out.println();
  15.  
  16.         //ArrayList
  17.         System.out.println("ArrayList:");
  18.         ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
  19.             //Insert values to ArrayList
  20.         weeklyTemperatures.add(78);
  21.         weeklyTemperatures.add(67);
  22.         weeklyTemperatures.add(89);
  23.         weeklyTemperatures.add(94);
  24.  
  25.         System.out.println("Slot 2 = " + weeklyTemperatures.get(1));
  26.  
  27.             //insert new values to ArrayList
  28.         System.out.println("Insert new value slot 2...");
  29.         weeklyTemperatures.add(1,111);
  30.         System.out.println("Slot 2 = " + weeklyTemperatures.get(1));
  31.  
  32.  
  33.             //iterate through ArrayList
  34.         System.out.println();
  35.         System.out.println("Iterating through the ArrayList");
  36.         for (int j = 0; j < weeklyTemperatures.size(); j++){
  37.             System.out.println(weeklyTemperatures.get(j));
  38.         }
  39.  
  40.  
  41.         //For Each Loop
  42.         System.out.println();
  43.         System.out.println("For Each Loop: ");
  44.         for (Integer temperature : weeklyTemperatures){
  45.             System.out.println(temperature);
  46.         }
  47.  
  48.  
  49.         //HashMap
  50.         System.out.println();
  51.         System.out.println("HashMap: ");
  52.         HashMap<String,Integer> restaurantMenu = new HashMap<String,Integer>();
  53.  
  54.         restaurantMenu.put("Turkey Burger", 13);
  55.         restaurantMenu.put("Naan Pizza", 11);
  56.         restaurantMenu.put("Cranberry Kale Salad", 10);
  57.  
  58.         System.out.println(restaurantMenu.size());
  59.  
  60.         for(String item : restaurantMenu.keySet()){
  61.             System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars");
  62.         }
  63.  
  64.  
  65.         //Summary Example
  66.         System.out.println();
  67.         System.out.println("Summary Example:");
  68.  
  69.         ArrayList<String> sports = new ArrayList<String>();
  70.         sports.add("Football");
  71.         sports.add("Boxing");
  72.  
  73.         for (String sport : sports){
  74.             System.out.println(sport);
  75.         }
  76.  
  77.             //Major cities and the year they were founded
  78.         System.out.println();
  79.         HashMap<String,Integer> majorCities = new HashMap<String,Integer>();
  80.  
  81.         majorCities.put("New York" , 1624);
  82.         majorCities.put("London" , 43);
  83.         majorCities.put("Mexico City" , 1521);
  84.         majorCities.put("Sao Paulo" , 1554);
  85.  
  86.         for (String city: majorCities.keySet()){
  87.             System.out.println(city + " was founded in " + majorCities.get(city));
  88.         }
  89.  
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement