2013/05/29面试

4 篇文章 0 订阅
1 篇文章 0 订阅

三个编程题:1.String的map转成二维数组。2.Integer的List排序。3.某个目录下文件类型总数。

1.

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;


public class Map2Array {

	public static void main(String[] args) {
		//Map m =new HashMap();  //显示的是3,2,1  
        Map<String,String> m =new LinkedHashMap<String, String>();  //显示的是1,2,3  
        m.put("1","aaaa");  
        m.put("2","baaa");  
        m.put("3","caaa"); 
        
        String[][] s = map2Array(m);
        if(s!=null)
        	for(String [] ss:s){
        		for(String sss:ss){
        			System.out.print(sss+" ");
        		}
        		System.out.println();
        	}
       
	}
	
	static String[][] map2Array(Map map) {
		int length = map==null?0:map.size();
		if(length==0){
			return null;
		}
		
		String[][] result= new String[length][2];
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		int i=0;
		while(iterator.hasNext()) {
			String key = (String) iterator.next();
			result[i][0]=key;
			result[i++][1] = (String) map.get(key);
		}
		return result;
	}
}

2

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ListSort {

	public static void main(String[] args) {
		List<Integer> oldList = new ArrayList<Integer>();
		oldList.add(1);
		oldList.add(5);
		oldList.add(8);
		oldList.add(2);
		System.out.println("开始: ");
		for(int i:oldList) {
			System.out.print(i+"-");
		}
		Collections.sort(oldList);
		System.out.println("\n升序: ");
		for(int i:oldList) {
			System.out.print(i+"-");
		}
		Collections.reverse(oldList);
		System.out.println("\n降序:");
		for(int i:oldList) {
			System.out.print(i+"-");
		}
		
		System.out.println("\n最大:");
		System.out.print(Collections.max(oldList));
		System.out.println("\n最小:");
		System.out.print(Collections.min(oldList));
	}
}

3.

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class CountKindsFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filePath = "F:\\Test";
		String fileType = "";
		File rootDirectory = new File(filePath);
		File[] files = rootDirectory.listFiles();
		System.out.println("总共有 "+files.length+" 个文件.");
		Map<String,Integer> m = new HashMap<String, Integer>();
		for(File f:files){
			if(f.isDirectory()) {
				if(m.containsKey("folder")) {
					m.put("folder", m.get("folder")+1);
				} else {
					m.put("folder", 1);
				}
			} else {
				fileType = f.getName().substring(f.getName().lastIndexOf(".")+1);
				if(m.containsKey(fileType)) {
					m.put(fileType, m.get(fileType)+1);
				} else {
					m.put(fileType, 1);
				}
			}
		}
		System.out.println("总共有 "+m.size()+" 种文件.");
		Set set = m.keySet();
		Iterator iterator = set.iterator();
		while(iterator.hasNext()) {
			String key = (String) iterator.next();
			Integer value = m.get(key);
			System.out.println(key+"有"+value+"个");
		}
	}

}





结果:


分割————————————————————————————————————————————————————————————————————

两个数据库题:

1.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选C.

2.查询出每门课都大于80分的学生姓名 

score表

name   subject score
张三     语文
       81
张三     数学
       75
李四     语文
       76  特意改为85
李四     数学
       90
王五     语文
       81
王五     数学
       100
王五     英语
       90


1.

select 
case 
when A>B then 
case when A>C then A 
else C end 
else 
case when B>C then B else C 
end 
end as name 
from member
详细看  猛击


2.在网上查询大量资料后,看到的情况都是李四的语文为76,特意改为85,按照网上的解决方案为:

select distinct name from score where name not in(
	select distinct name from score where score<80
)


得到的结果为:李四和王五。

从题目中得知张三和李四是没有英语成绩,再怎么也不会出现在最后的结果里。

故我所写的有点繁琐:

select name
from(
select name,
max(case subject when '语文' then score end) as yuwen,
max(case subject when '数学' then score end) as shuxue,
max(case subject when '英语' then score end) as en
from score
group by name) temp
where yuwen>80 and shuxue>80 and en>80;
若有好的SQL尽情贴上。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值