会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134168个问题

# 数据结构:哈希表,使用二次探查方法解决哈希冲突的问题

# 先创建数组
class Array(object):
    def __init__(self,size=5):
        self._size = size # 存储数量
        self._item = [None]*size # 数组空间大小
        self._length = 0

    # 设置值
    def __setitem__(self, key, value):
        self._item[key]=value
        self._length += 1
    # 获取值
    def __getitem__(self, key):
        return self._item[key]

    # 定义长度单位
    def __len__(self):
        return self._length

    # 定义迭代特性
    def __iter__(self):
        for v in self._item:
            yield v

    def delitem(self,key):
        if self._item[key]:
            self._item[key]=None
        else:
            print('本身无数据')

# 创建存储到哈希表的对象类型
class Solt():
    # 初始化方法
    def __init__(self,key=None,value=None):
        self.key = key
        self.value = value
    # 打印对象
    def __str__(self):
        return f'key : {self.key},value : {self.value}'

# 创建哈希表数据结构,此方法存在哈希冲突
class HashTable():
    # 初始化方法 生成固定长度的哈希表
    def __init__(self):
        self.size = 4
        self.item = Array(self.size)

    # 获取哈希索引的方法
    def get_index(self,key):
        return hash(key) % self.size

    # 插入数据时查找哈希地址是否被占用,返回为空的地址索引
    def find_indexToPut(self,key):
        # 获取输入的key对应的索引值
        index = self.get_index(key)
        if self.item[index] == None:
            return index
        else:
            # 循环判断索引对应的对象的分支
            while self.item[index] is not None:
                if self.item[index].key == key:
                    return index
                else:
                    index = (5 * index + 1) % self.size
            return index

    # 放入哈希表数据
    def put(self,key,value):
        s = Solt(key,value)
        index = self.find_indexToPut(key)
        self.item[index] = s

    # 获取哈希表数据时,先判断索引对应位置存储的对象
    def find_index(self,key):
        index = self.get_index(key)
        # 循环判断索引对应的对象的分支
        if self.item[index] == None:
            return None
        else:
            # 循环判断索引对应的对象的分支
            while self.item[index] is not None:
                if self.item[index].key == key:
                    return index
                else:
                    index = (5 * index + 1) % self.size
            return None

    # 获取哈希表数据
    def get(self,key):
        # 判断获取的index
        index = self.find_index(key)
        return self.item[index]



if __name__ == '__main__':
    ht = HashTable()
    ht.put('name','我的老伙计')
    ht.put('sex', '男')
    ht.put('age', 18)
    ht.put('work', '程序员')
    ht.put('name', '北京')  # 超出哈希表的容量,陷入死循环了
    # ht.put('money', 1000)
    print(ht.get('name'))
    print(ht.get('sex'))
    print(ht.get('age'))
    print(ht.get('work'))
    # print(ht.get('home'))


Python全系列/第十七阶段:数据结构与算法/算法与数据结构(旧) 20866楼

ego-shop.7z

图片.png将index保存至localstorage中,跳转至规格参数页面后,直接在地址栏修改地址,跳转之后,就这样了,该怎么解决?

WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/Ego商城高级Vue实战项目 20867楼
JAVA 全系列/(隐藏)第三十阶段:设计模式/框架源码分析(拓展)/SpringMVC源码分析 20873楼

fl_se_67.zip

屏幕截图(27).png请问我这是什么问题


Python全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 20875楼
Python全系列/ 第十五阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 20876楼

问题在哪呀

package com.llz.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class JdbcUtil {
    private static String driver;
    private static String JdbcUrl;
    private static String userName;
    private static String userPassword ;
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("Jdbc");
        driver = bundle.getString("driver");
        JdbcUrl = bundle.getString("JdbcUrl");
        userName = bundle.getString("userName");
        userPassword = bundle.getString("userPassword");
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取connection对象
    public static Connection getConnection() {
        Connection conn = null;

        try {
            conn = DriverManager.getConnection(JdbcUrl, userName, userPassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    //关闭
    public static void closeState(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
    public static void closeConn(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

}
package com.llz.jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

class JdbcTest{
    public void test(int id,String address,int amount){
        Connection conn = null;
        Statement state = null;
        try{
            conn = JdbcUtil.getConnection();
            String sql = "insert into goodcount values('"+id+"','"+address+"','"+amount+"')";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.closeState(state);
            JdbcUtil.closeConn(conn);
        }
    }
    public void updateTest(int id,String address,int amount){
        Connection conn = null;
        Statement state = null;
        try{
            conn=JdbcUtil.getConnection();
            String sql = "update goodcount set id = '"+id+"',address='"+address+"' where amount="+amount+"";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.closeConn(conn);
            JdbcUtil.closeState(state);
        }
    }
}
public class StatementTest {
    public static void main(String[] args) {

        JdbcTest jt = new JdbcTest();
        jt.test(6,"重庆",5);
        //jt.updateTest(8,"湖南",5);
    }
}
driver = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://localhost:3306/good?useUnicode=true&characterEncoding=utf-8
userName = root
userPassword =123456


JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 20877楼
Python全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 20879楼
Python全系列/第六阶段:数据库与AI协同技术实战/mysql的使用 20880楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637