Java 4

Primero corregimos un bucle for

public class For {
 public static void main(String[] args) {
  for (int waterLevel = 0; waterLevel < 7; waterLevel++) {
   System.out.println("The pool's water level is at " + waterLevel + " feet.");
  }
 
 }
}



Después vamos creando un código desde cero donde añadimos cosas a la lista weeklytemperatures y se muestran de uno en uno.

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) );
  }
}

}



Ahora con un hashmap creamos el siguiente código que  nos dice los platos del menú y su regalo


import java.util.HashMap;

public class RestaurantForEach {
public static void main(String[] args) {

  HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();

  restaurantMenu.put("Turkey Burger", 13);
  restaurantMenu.put("Naan Pizza", 11);
  restaurantMenu.put("Cranberry Kale Salad", 10);

  System.out.println(restaurantMenu.size());
  for (String item : restaurantMenu.keySet()) {
   System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
  }
}

}


Luego mezclamos listas y hashmap ya que hacemos una lista de deportes y un hashmap de ciudades y su año de fundación.



public class GeneralizationsD {
public static void main(String[] args) {
  ArrayList<String> sports = new ArrayList<String>();
    sports.add("Football");
  sports.add("Boxing");
  for(String sport : sports) {
   System.out.println(sport);
  }

  //Major cities and the year they were founded
  HashMap<String, Integer> majorCities = new HashMap<String, Integer>();

  majorCities.put("New York", 1624);
  majorCities.put("London", 43);
  majorCities.put("Mexico City", 1521);
  majorCities.put("Sao Paulo", 1554);

  for (String city : majorCities.keySet() ) {

   System.out.println(city + " was founded in " + majorCities.get(city));

  }
}

}




Comentarios

Publicacións populares deste blog

Javascript

PYTHON