java and python equivalent of php's foreach($array as $key => $value)

前端 未结 8 970
抹茶落季
抹茶落季 2020-12-20 11:56

In php, one can handle a list of state names and their abbreviations with an associative array like this:



        
8条回答
  •  隐瞒了意图╮
    2020-12-20 12:27

    in java for associative array use Map

    import java.util.*;
    
    class Foo
    {
        public static void main(String[] args)
        {
            Map stateMap = new HashMap();
            stateMap.put("ALABAMA", "AL");
            stateMap.put("ALASKA", "AK");
            // ...
            stateMap.put("WYOMING", "WY");
    
            for (Map.Entry state : stateMap.entrySet()) {
                 System.out.printf(
                    "The abbreviation for %s is %s%n",
                    state.getKey(),
                    state.getValue()
                );
            }
        }
    }
    

提交回复
热议问题