This video is interpretive , So I hope you have read the content of this knowledge point , And after writing the corresponding code , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning
![]() 3 branch 7 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers Example 1 : Limitations of using arrays Example 2 : ArrayList Storage object
If you want to store multiple objects , You can use arrays , But arrays have limitations
For example The declaration length is 10 Array of Unused arrays are wasted exceed 10 Number of , I can't let go
package collection; import charactor.Hero; public class TestCollection { public static void main(String[] args) { // Limitations of arrays Hero heros[] = new Hero[10]; // The declaration length is 10 Array of // Unused arrays are wasted // exceed 10 Number of , I can't let go heros[0] = new Hero(" Galen "); // If you can't put it down, you have to report an error heros[20] = new Hero(" Timo "); } }
package charactor; public class Hero { public String name; public float hp; public int damage; public Hero() { } // Add an initialization name The construction method of public Hero(String name) { this.name = name; } // rewrite toString method public String toString() { return name; } }
To address the limitations of arrays , Introduce the concept of container class . The most common container class is
ArrayList The capacity of the container "capacity" As the number of objects increases , Automatic growth Just keep adding heroes to the container , Don't worry about the boundary of the array .
package collection; import java.util.ArrayList; import charactor.Hero; public class TestCollection { @SuppressWarnings("rawtypes") public static void main(String[] args) { // Container class ArrayList, Used to store objects ArrayList heros = new ArrayList(); heros.add( new Hero(" Galen ")); System.out.println(heros.size()); // The capacity of the container "capacity" As the number of objects increases , Automatic growth // Just keep adding heroes to the container , Don't worry about the boundary of the array . heros.add( new Hero(" Timo ")); System.out.println(heros.size()); } }
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2020-08-09
Java aggregate In combination with JDK Notes recorded by the source code , I hope I can help you
2 One answer
ahead Jump to the problem location Answer time :2021-03-28
hxd The notes hang up
pentacf Jump to the problem location Answer time :2021-03-03
The notes hang up , brother
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2020-06-27
You can also understand directly with this code arraylist
2 One answer
stone520 Jump to the problem location Answer time :2020-10-16
The string itself is an object , It is not a basic data type , You put two strings , It's actually two string objects
No pulp, all waves Jump to the problem location Answer time :2020-09-11
This one doesn't add new Hero() If not, the object is not stored , It's a string
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2020-03-31
Why output the whole List When I was Don't rewrite tostring() Method will output the address of the object Instead of attribute values
2019-04-03
list Add questions
2019-03-25
What's going on , Prompt missing class How to deal with it, big guys
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 8 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|