欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Data Structures

程序员文章站 2024-03-17 23:37:22
...

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) + ".");

            }
        }

    }

}