• 作业


     

    Day01_统计字符串中重复字符出现个数 (集合联系map)

    package com_01_testfan_02_basic;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class Day01_06_作业 {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		/*
    		 *需求:1.统计字符串中重复单词出现个数 (集合联系map)
    		 *    2.数据demo如22dddddscvllljkkldfff
    		 */
    		
    		String str_processedString = "22dddddscvllljkkldfff";
    		Map<Character, Integer> map = new HashMap<Character, Integer>();
    		//将字符放入临时字典中
    		char[] ar = str_processedString.toCharArray();
    		for (char c : ar) {
    			//System.out.println(c);
    			if (!map.containsKey(c)) {
    				map.put(c, 1);
    			} else {
    				map.put(c, map.get(c)+1);
    			}
    		}
    		//从临时字典中将数据取出来
    		Set<Character> set = map.keySet();
    		for (Character character : set) {
    			System.out.println(character + " " + map.get(character));
    		}
    		
    	}
    
    }
    

     

    老师代码

    TreeMap 进行排序

    LinkedHashMap 先进先出

    HashMap 无序

    package com_01_testfan_02_basic;
    
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    public class Day01_06_作业2 {
        
        
        public static void main(String[] args) {
            String testString="2342a2sfghgyu56asdasdaddddd";
            //0 //length()-1
            //char char_str =testString.charAt(testString.length()-1);
            //Map<String, Integer> mapcount = new LinkedHashMap<String, Integer>();
            //Map<String, Integer> mapcount = new TreeMap<String, Integer>();
            Map<String, Integer> mapcount = new HashMap<String, Integer>();
            
            for (int i = 0; i < testString.length(); i++) {
                //System.out.println(testString.charAt(i));
                String key =String.valueOf(testString.charAt(i));
                //如果map 不存在 value 1
                if(!mapcount.containsKey(key)) {
                    mapcount.put(key, 1);
                }else {
                //如果存在
                    int value = mapcount.get(key);
                    value +=1;
                    mapcount.put(key, value);
                }
            }
            
            Set<String> keySet =mapcount.keySet();
            for (String key : keySet) {
                System.out.println(key +" "+mapcount.get(key));
            }
            
            //System.out.println(char_str);
        }
    
    }

    Day02_数据存放在硬盘中

    将数据库获取的数据存放在硬盘中

    package com.testfan.db;
    
    //数据库模拟对象 
    public class DbUser {
        private String uid;
        private String loginname;
        private String loginpass;
        public String getUid() {
            return uid;
        }
        public void setUid(String uid) {
            this.uid = uid;
        }
        public String getLoginname() {
            return loginname;
        }
        public void setLoginname(String loginname) {
            this.loginname = loginname;
        }
        public String getLoginpass() {
            return loginpass;
        }
        public void setLoginpass(String loginpass) {
            this.loginpass = loginpass;
        }
        @Override
        public String toString() {
            //return "DbUser [uid=" + uid + ", loginname=" + loginname + ", loginpass=" + loginpass + "]";
            return "";
        }
    
    }
    package com.testfan.db;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    public class DbTest {
        
        public static void main(String[] args) throws Exception {
            String path = "D:\user.csv";
            List<DbUser> list = getAllDbUserTest();
            //System.out.println(list.size());
            for (DbUser dbUser : list) {
                String nameString = dbUser.getLoginname();
                String uid = dbUser.getUid();
                String pass = dbUser.getLoginpass();
                String resultLine = nameString +","+ uid +","+ pass;
                writeToFile(resultLine, path);
                //System.out.println(resultLine);
            }
            //String line = "abdd";
        
    
        }
    
    
        //jdk 原始代码
        public static List<DbUser> getAllDbUserTest(){
            Connection conn = null;
            Statement statement = null;
            List<DbUser> list =null;
            try {
                //找一个工具去连接数据库
                Class.forName("com.mysql.cj.jdbc.Driver");
                //连接数据库
                conn = DriverManager.getConnection
                ("jdbc:mysql://118.24.13.38:3308/test?characterEncoding=utf8&useSSL=false", "zhangsan", "123123");
                
                //准备sql
                String  sql= "select * from t_user_test";
                
                //创建执行sql对象
                statement = conn.createStatement();
                //执行sql, 获取返回结果集合
                ResultSet  set=statement.executeQuery(sql);
                list = new ArrayList<DbUser>();
                while (set.next()) {
                    DbUser user = new DbUser();
                    user.setUid(set.getString("uid"));
                    user.setLoginname(set.getString("loginname"));
                    user.setLoginpass(set.getString("loginpass"));
                    list.add(user);
                    //System.out.println(set.getString("uid")+" "+set.getString("loginname")+" "+set.getString("loginpass"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //数据库关闭
                try {
                    conn.close();
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
    
        
        
        /**
         *  功能:将数据追加写入文本文件中
         * @param line
         * @throws IOException
         */
        private static void writeToFile(String line, String path) throws IOException {
            BufferedWriter br= new BufferedWriter(new FileWriter(path, true));
            br.write(line);
            br.newLine();
            br.close();
        }
        
    }

    老师代码如下

  • 相关阅读:
    角色扮演游戏引擎的设计原理
    游戏服务器架构
    小谈网络游戏同步
    What is the single most influential book every programmer should read?
    Research Scientists and Engineers
    关于为什么不推荐使用用户定义表类型的说明
    程序员必须遵守的编程原则
    CacheStrategy缓存
    正能量
    MEF 和 MAF
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/10964522.html
Copyright © 2020-2023  润新知