面向对象编程的概念及特性 英文描述整理

这段代码展示了使用Java进行面向对象编程,通过BufferedReader读取文件,并将文件内容存储到两个HashMap中,实现了数据的双向映射。HashMap分别用于存储基于名字和号码的数据集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第七题

private String[] strs = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> list=new ArrayList<String>();
        if(digits.length()==0){
            return list;
        }
        if(digits.length()==1){
            int index=Integer.parseInt(digits)-2;
            String x=strs[index];
            char[] c=x.toCharArray();
            for(int i=0;i<c.length;i++){
                list.add(""+c[i]);
            }
            return list;
        }
        String newdigits=digits.substring(1);
        String indStr=digits.substring(0,1);
        int indInt=Integer.parseInt(indStr)-2;
        String xx=strs[indInt];
        char[] cc=xx.toCharArray();
        List<String> result=letterCombinations(newdigits);
        for(int j=0;j<cc.length;j++){
            for(int m=0;m<result.size();m++){
                list.add(""+cc[j]+result.get(m));
            }
        }
        return list;
        
    }



读文件



import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;




public class Interview {


private HashMap<String, HashSet<String>> numberMap = new HashMap<String, HashSet<String>>();

private HashMap<String, HashSet<String>> nameMap = new HashMap<String, HashSet<String>>();

public static void main(String[] args){

Interview interview = new Interview();
interview.readFileIntoParam(args[0]);


}


public void readFileIntoParam(String fileName) {
try {  
           BufferedReader reader = new BufferedReader(new FileReader(fileName));
           reader.readLine(); 
           String line = null;  
           while((line=reader.readLine())!=null){  
               String item[] = line.split(",");
               if(nameMap.get(item[0])!=null) {
                HashSet tmp = nameMap.get(item[0]);
                tmp.add(item[1]);
               }else {
                HashSet tmp = new HashSet();
                tmp.add(item[1]);
                nameMap.put(item[0], tmp);
               }
               
               if(numberMap.get(item[1])!=null) {
                HashSet tmp = numberMap.get(item[1]);
                tmp.add(item[0]);
               }else {
                HashSet tmp = new HashSet();
                tmp.add(item[0]);
                numberMap.put(item[1], tmp);
               }
              
           }  
       } catch (Exception e) {  
           e.printStackTrace();  
       }  
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值