网易云课堂java进阶第三周作业

本文介绍了一个使用Java HashMap解决城市间距离查询问题的实例。通过读取标准输入,将城市名称映射到整数标识,再建立城市间的距离矩阵,最后查询并输出两个指定城市之间的距离。该方法展示了如何有效利用HashMap进行数据管理和快速查询。

http://mooc.study.163.com/learn/ZJU-1000004001?tid=1000005000#/learn/ojhw?id=1000215000

 1 package J3;
 2 import java.util.*;
 3 public class Test2 {
 4     private HashMap<String, Integer> city = new HashMap<String, Integer>();
 5     private int cityAmount = 0;
 6     
 7     public void init(){
 8         Scanner in = new Scanner(System.in);
 9         String next = new String();
10         while( !(next = in.next()).equals("###")){
11             
12             city.put(next, cityAmount);
13             cityAmount++;
14         }
15 //        System.out.println(cityAmount);
16         int[][] table = new int[cityAmount][cityAmount];
17         for( int i = 0; i < cityAmount; i++ ){
18             for( int j = 0; j < cityAmount; j++){
19                 table[i][j] = in.nextInt();
20             }
21         }
22         String compareA = new String();
23         String compareB = new String();
24         compareA = in.next();
25         compareB = in.next();
26         in.close();
27         System.out.println(table[city.get(compareA)][city.get(compareB)]);
28     }
29     
30     public static void main(String[] args){
31         new Test2().init();
32     }
33 }

哎,没有思路时,首先想到的还是过程化解决问题,,,还是读书少啊。。

转载于:https://www.cnblogs.com/happi1y/p/4755496.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值