国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

幾個面試題

2019-11-10 22:38:07
字體:
來源:轉載
供稿:網友

第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); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白城市| 德清县| 益阳市| 西青区| 临潭县| 高邑县| 油尖旺区| 舒兰市| 石屏县| 万盛区| 丰台区| 姜堰市| 武城县| 金沙县| 红原县| 舒城县| 普兰县| 安多县| 嘉黎县| 上杭县| 乌兰察布市| 广州市| 巴青县| 阿瓦提县| 丰原市| 肃宁县| 新余市| 舞钢市| 皋兰县| 龙陵县| 达孜县| 清原| 华安县| 龙游县| 出国| 清流县| 萨嘎县| 武山县| 上高县| 寿光市| 卫辉市|