Data Structures

2018-02-04  本文已影响0人  我是阿喵酱

Data Structures
Learn to create, manipulate, and store information in data structures.

Instance

import java.util.*;

public class Olympics {

    public static void main(String[] args) {

        //Some Olympic sports 

        ArrayList<String> olympicSports = new ArrayList<String>();
        olympicSports.add("Archery");
        olympicSports.add("Boxing");
        olympicSports.add("Cricket");
        olympicSports.add("Diving");

        System.out.println("There are " + olympicSports.size() + " Olympic sports in this list. They are: ");

        for (String sport: olympicSports) {
            System.out.println(sport);
        }

        //Host cities and the year they hosted the summer Olympics

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

        hostCities.put("Beijing", 2008);
        hostCities.put("London", 2012);
        hostCities.put("Rio de Janeiro", 2016);

        for (String city: hostCities.keySet()) {
            
            if (hostCities.get(city) < 2016) {

                System.out.println(city + " hosted the summer Olympics in " + hostCities.get(city) + ".");

            } else {

                System.out.println(city + " will host the summer Olympics in " + hostCities.get(city) + ".");

            }
        }

    }

}
上一篇下一篇

猜你喜欢

热点阅读