会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132437个问题
JAVA 全系列/第二十一阶段:分布式医疗云平台/项目环境搭建(旧) 19606楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 19607楼
JAVA 全系列/第二十阶段:租房网(Spring Cloud最新架构)/Livegoods第一天 19609楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 19610楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 19611楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 19613楼

老师好,我在应用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 基础深化和提高/容器(旧) 19614楼

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 基础深化和提高/常用类 19616楼

fastdsfdemo.rar

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

图片.png

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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