会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134273个问题
JAVA 全系列/第二十三阶段:分布式医疗云平台/项目环境搭建(旧) 19876楼

import  numpy as np
import pymysql
 
def insertData(numpy_bytes,shape_str):
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
 
    cur = db.cursor()
    #游标对象
 
    sql = "insert into face_data(numpy_data,shape) values(%s,%s)"
    #定义好sql语句,%s是字符串的占位符
 
    try:
        cur.execute(sql,(numpy_bytes,shape_str))
        #执行sql语句
        db.commit()
        #提交到数据库中
    except Exception as e:
        #捕获异常
        raise e
    finally:
        db.close()  # 关闭连接

def upData(numpy_bytes,shape_str):    
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
     
    # 使用cursor()方法获取操作游标
    cur = db.cursor()
     
    sql_update1 ="update face_data set numpy_data = %s where id = %d"
    sql_update2 ="update face_data set shape = %s where id = %d"
    try:
        cur.execute(sql_update2 % (shape_str,12))  #像sql语句传递参数
    	cur.execute(sql_update1 % (numpy_bytes,12))  #像sql语句传递参数
    	#提交
    	db.commit()
    except Exception as e:
    	#错误回滚
    	db.rollback() 
    finally:
    	db.close()
        
    

def readData():
    db = pymysql.connect(host="localhost", user="root", password="19950127", db="test", port=3306)
    #连接数据库对象
 
    cur = db.cursor()
    #游标对象
 
    sql = "select * from face_data"
    #定义好sql语句,%s是字符串的占位符
 
    try:
        cur.execute(sql)
        #执行sql语句
        results = cur.fetchall()
        #获取所有结果集
        for row in results:
            numArr = np.fromstring(string=row[1], dtype=int)
            #将读取到的字节流转化为ndarray数组
 
            shape = tuple(eval(row[2]))
            #将读取到的shape转换为元组的格式,这里的eval(),由于我们元组里面的数据是int的所以,这里eval()的作用就是把本该是数字的转化回来
            numArr.shape = shape
            #设置维度,设置的数值为元组
            print(numArr)
        db.commit()
        #提交到数据库中
    except Exception as e:
        #捕获异常
        raise e
    finally:
        db.close()  # 关闭连接
 
if __name__ == '__main__':
    arr =np.arange(5, 50).reshape(3,5,3)
    #生产0-45数字的维度=(3,5,3)的三维数组
 
    shape_ = arr.shape
    #获取数组的维度
 
    numpy_bytes = arr.tostring()
    #将数组转化为二进制流
 
    shape_str = "".join(str(shape_))
    #将shape元组转化为字符串
 
    #insertData(numpy_bytes, shape_str)
    #插入数据库
    upData(numpy_bytes, shape_str)
    #更新数据库
    readData()
    #读取数据库

老师您好,我想尝试着储存np数组。将数组转化为bytes格式的二进制流后,在插入和读取部分都没有问题,但是在更新数据库时遇到了很大的问题,是占位符不对吗?您能帮我看下吗,麻烦了!

Python全系列/第六阶段:数据库与AI协同技术实战/python操作mysql(旧) 19877楼
JAVA 全系列/第二十三阶段:分布式医疗云平台/项目环境搭建(旧) 19879楼
Python全系列/ 第十五阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 19880楼
JAVA 全系列/第二十二阶段:租房网(Spring Cloud最新架构)/Livegoods第一天 19882楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 19883楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 19884楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 19886楼

老师好,我在应用ArrayList练习算法时遇到如下问题:


题目如下:

https://leetcode-cn.com/problems/pascals-triangle/


我写的代码如下:


class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res  =new ArrayList<>();
        List<Integer> list= new ArrayList<>();
        for(int i=0;i<=(numRows-1); i++){
            list.add(0,1);
            for(int j=1; j<= (list.size()-2); j++){
                list.set(j, list.get(j)+list.get(j+1));
            }
            res.add(new ArrayList<>(list)); 
        }
        return res;
    }
}



我有两个问题:

(1)第十行

res.add(new ArrayList<>(list));

我不懂为什么这里要新实例化一个ArrayList,因为list本身就是arraylist类型,这里为什么不能直接添加?即


res.add(list);

这样写,程序本身可以执行,但是结果有错,

[[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1]]

而正确的结果是:

[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]


所以错误结果,相当于把最后一次list输出了5遍,我不懂为什么造成这样结果?




(2)关于数组和arrayList的区别:

我在写第八行时,想写

list[j]=list[j] + list[j+1];


但是不对,这里我对数组和arraylist混了,我想问下这两种底层不都是应用数组的方法储存数据吗?为什么取数据的方法没有通用,两个到底是一个什么关系?arraylist接口下的一个实现类,那么数组也是一种类吗?谢谢

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 19887楼

package integre.fang.com;
/**
 * 自己手写得包装类
 *
 * */
public class MyInteger01 {
    private int value;
    public static final int LOW=-128;
    public static final int HIGH=127;
    private static MyInteger01[] myInteger01;

    static {
        for (int i=LOW;i<=HIGH;i++){
          myInteger01[i+128]=MyInteger01.getMyinteger(i);//核心得点在这里,有点迷糊
        }
    }

    public  MyInteger01(int a) {
        this.value = a;
    }

    public static MyInteger01 getMyinteger(int a){
        if (LOW<=a && a<=HIGH){
            return myInteger01[a+128];
        }
            return new MyInteger01(a);

    }

    public static void main(String[] args) {
        int a=100;
        int b=100;
        MyInteger01 m1=new MyInteger01(300);
        MyInteger01 m2=new MyInteger01(300);
        MyInteger01 m3=new MyInteger01(100);
        MyInteger01 m4=new MyInteger01(100);
        System.out.println(m1==m2);
        System.out.println(m3==m4);
        //System.out.println(MyInteger01.getMyinteger(a)==MyInteger01.getMyinteger(b));
        //System.out.println(MyInteger01.getMyinteger(a).equals(MyInteger01.getMyinteger(b)));

        int c=1000;
        int d=1000;
        //System.out.println(MyInteger01.getMyinteger(c)==MyInteger01.getMyinteger(d));
        //System.out.println(MyInteger01.getMyinteger(c).equals(MyInteger01.getMyinteger(d)));
    }

}

Exception in thread "main" java.lang.ExceptionInInitializerError

老师帮忙看看哪里出错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 19889楼

fastdsfdemo.rar

做fastdsf项目,一直报这个错,说是找不到方法,老师帮忙看一下

图片.png

JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/FastDFS 19890楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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