当前位置:网站首页>Remember the use of add method once
Remember the use of add method once
2022-07-02 10:07:00 【SpongeBob's nest】
I came across a relatively simple question when I was doing the question today , But the car overturned , After debugging for a long time, I didn't find the cause of the error , The output result is always empty
Suddenly I had a flash of inspiration , Could it be this add There is a problem with your method , After all, there is nothing wrong with the code logic .
So I checked the information, but I didn't get anything ……
I caught a glimpse of a search bar add Method is a copied reference or a value . I suddenly realized , Soon! ,
I left a set , The right set sets , And then run , Ha , That's true
public static void main(String[] args) {
ArrayList<Integer>list = new ArrayList<>();
ArrayList<ArrayList<Integer>>result = new ArrayList<>();
result.add(list);
for(int i = 0; i < 5; i++){
list.add(i);
System.out.println(" Results to be selected "+list);
System.out.println(" Result set :"+result);
System.out.println("========================");
}
}
We can clearly see that we put list Has been added to result in , however list After adding elements in ,result The elements in have also changed , This explanation add What's coming in is list References to , It's not worth it .
public static void main(String[] args) {
ArrayList<Integer>list = new ArrayList<>();
ArrayList<ArrayList<Integer>>result = new ArrayList<>();
result.add(list);
for(int i = 0; i < 5; i++){
list.add(i);
System.out.println(" Results to be selected "+list);
System.out.println(" Result set :"+result);
list.remove(list.size()-1);
System.out.println("result change "+result);
System.out.println("========================");
}
}
So , After going through a lot of hardships, the ninety-nine and eighty-one difficulties , I finally solved this problem
Attach a link to the original title : Find the path where the sum of binary tree values is a specific value
Simple backtracking algorithm .
error (×):
correct (√):
Okay , This mistake will serve as a lesson …………
边栏推荐
- Required request body is missing: (cross domain problem)
- Centos7 one click compilation and installation of PHP script
- Read Day6 30 minutes before going to bed every day_ Day6_ Date_ Calendar_ LocalDate_ TimeStamp_ LocalTime
- Tee command usage example
- It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
- Career planning and development
- MySQL transaction
- Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
- UE4夜间打光笔记
- The road is blocked and long, and the line is coming
猜你喜欢
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
2837xd 代码生成——补充(2)
What is the relationship between realizing page watermarking and mutationobserver?
MySQL index
Skywalking theory and Practice
Record the interesting process of using Xray for the first time
2837xd code generation - Supplement (2)
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
2837xd 代码生成——StateFlow(1)
vs+qt 设置应用程序图标
随机推荐
2837xd 代码生成——总结篇
YOLO物体识别,生成数据用到的工具
Share a blog (water blog)
Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
int与string、int与QString互转
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
渗透测试的介绍和防范
2837xd code generation - Summary
Eslint reports an error
Sil/pil test of matlab code generation
PI control of grid connected inverter (grid connected mode)
Bugkuctf-web16 (backup is a good habit)
Bugkuctf-web24 (problem solving ideas and steps)
Introduction and prevention of penetration test
Project practice, redis cluster technology learning (VII)
Ckeditor 4.10.1 upload pictures to prompt "incorrect server response" problem solution
What is the relationship between realizing page watermarking and mutationobserver?
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
How does {} prevent SQL injection? What is its underlying principle?