首页 > 学院 > 开发设计 > 正文

几个面试题

2019-11-10 21:26:30
字体:
来源:转载
供稿:网友

第1题

package com.demo;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.Set;/* 数组String[] strs中有很多重复的元素,找出出现最多的 元素,并返回出现的次数,(30分) */public class Test1 { public static void main(String[] args) { String[] strs={"张无忌","赵敏","aaa","小昭","张无忌","赵敏","aaa","张无忌"}; Map<String,Integer> map = new HashMap<>(); for (String data : strs) { //检查map中是否包括data if(map.containsKey(data)){ //把对应的value加1 Integer count = map.get(data); count++; map.put(data, count); }else{ //不包括 map.put(data, 1); } } System.out.PRintln(map); Collection<Integer> values = map.values(); List<Integer> list = new ArrayList<>(values); Collections.sort(list); int count = list.get(list.size()-1); Set<Entry<String, Integer>> entrySet = map.entrySet(); for (Entry<String, Integer> entry : entrySet) { if(entry.getValue() == count){ System.out.println(entry.getKey()+"-->"+entry.getValue()); break; } } }}

第2题

package com.demo;import java.sql.Connection;/* 有n个SQL任务List<SQL> task需要执行update,写一个方法 要求最多100条SQL执行一次事务提交commit,直到执行完毕,不漏任务 用到的知识点 批量的概念 */import java.sql.PreparedStatement;public class Test2 { public static void main(String[] args) { test2(); } //用jdbc的批量方法 private static void test2() { long start =System.currentTimeMillis(); //向表中添加1000条数据 String sql="insert into dept(dname,loc) values(?,?)"; Connection conn =DaoFactory.getConnection(); PreparedStatement stmt = null; try { conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); for(int i=1; i<=1000; i++){ stmt.setString(1, "a"+i); stmt.setString(2, "b"+i); stmt.addBatch(); //添加的缓存 if( i % 100 == 0 ){ stmt.executeBatch();//执行批量的语句 stmt.clearBatch();//清空缓存 } } conn.commit(); } catch (Exception e) { e.printStackTrace(); }finally{ DaoFactory.closeAll(null, stmt, conn); } long end =System.currentTimeMillis(); System.out.println( end-start ); //1503毫秒 } private static void test1() { long start =System.currentTimeMillis(); //向表中添加1000条数据 String sql="insert into dept(dname,loc) values(?,?)"; Connection conn =DaoFactory.getConnection(); PreparedStatement stmt = null; try { stmt = conn.prepareStatement(sql); for(int i=0; i<1000; i++){ stmt.setString(1, "a"+i); stmt.setString(2, "b"+i); stmt.executeUpdate(); } } catch (Exception e) { e.printStackTrace(); }finally{ DaoFactory.closeAll(null, stmt, conn); } long end =System.currentTimeMillis(); System.out.println( end-start ); //21503毫秒 }}

第3题

package com.demo;import java.util.ArrayList;import java.util.List;/* 1.写一个方法,将1000元现金随机发给20个客户, 每个客户的金额在30~100之间(30分) */public class Test3 { public static void main(String[] args) { boolean flag = true; while (flag) { int sum = 0; List<Integer> list = new ArrayList<Integer>(); for (int i = 1; i <= 20; i++) { // 产生的随机数 int num = (int) (Math.random() * 70 + 30); // 0---1 0.999999 sum += num;// 总金额 list.add(num); if (sum == 1000 && i == 20) { System.out.println(list); flag = false; } } } }}//如果是小额带两位小数的话可以对随机数格式化保留两位小数package com.demo;import java.text.DecimalFormat;import java.text.NumberFormat;public class Test4 { public static void main(String[] args) { double num = Math.random(); System.out.println(num); System.out.println(String.format("%.2f", num)); System.out.format("%.2f/n", num); DecimalFormat format = new DecimalFormat("0.00"); String strNum = format.format(num); System.out.println(strNum); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表