当前位置:网站首页>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 …………
边栏推荐
- Remember a simple Oracle offline data migration to tidb process
- Record personal understanding and experience of game console configuration
- Ckeditor 4.10.1 upload pictures to prompt "incorrect server response" problem solution
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
- Tinyxml2 reading and modifying files
- cmake的命令-官方文档
- 2837xd代码生成模块学习(4)——idle_task、Simulink Coder
- go语言入门
- TD联合Modelsim进行功能仿真
- C language: making barrels
猜你喜欢
Matlab生成dsp程序——官方例程学习(6)
Navicat remote connection MySQL reports an error 1045 - access denied for user 'root' @ '222.173.220.236' (using password: yes)
2837xd code generation - stateflow (2)
Image recognition - Data Acquisition
2837xd 代码生成——StateFlow(1)
2837xd code generation - stateflow (3)
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
Off grid control of three-phase inverter - PR control
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
[ue5] animation redirection: how to import magic tower characters into the game
随机推荐
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
Tee command usage example
The road is blocked and long, and the line is coming
Image recognition - data augmentation
ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
XA Transaction SQL Statements
Career planning and development
2837xd code generation module learning (2) -- ADC, epwm module, timer0
Required request body is missing: (cross domain problem)
2837xd code generation - stateflow (1)
Project practice, redis cluster technology learning (VII)
TD联合Modelsim进行功能仿真
How does {} prevent SQL injection? What is its underlying principle?
QT qlabel style settings
2837xd code generation module learning (1) -- GPIO module
Project practice, redis cluster technology learning (IX)
Is the C language too fat
2837xd代码生成模块学习(3)——IIC、eCAN、SCI、Watchdog、eCAP模块
Record the interesting process of using Xray for the first time
Cmake command - Official Document