inotgo.com

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 22 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 : Do not use generics   
Example 2 : Use generics   
Example 3 : Subclass object   
Example 4 : Short for generic   
Example 5 : practice - Generic   
Example 6 : answer - Generic   

The problem of not using generics
ADHero( Physical attack hero ) APHero( Magic attack hero ) It's all Hero Subclasses of
ArrayList Accept by default Object Object of type , So all objects can be put into ArrayList In
So get(0) The return type is Object
next , You need to cast to get APHero Type or ADHero type .
If software developers remember better , can Remember which is which , It's ok . But developers make mistakes , For example, the second 20 that 's ok , Will remember wrong , Put the second 0 Convert objects to ADHero, This will cause a type conversion exception
package generic; import java.util.ArrayList; import charactor.ADHero; import charactor.APHero; public class TestGeneric { public static void main(String[] args) { ArrayList heros = new ArrayList(); heros.add(new APHero()); heros.add(new ADHero()); APHero apHero = (APHero) heros.get(0); ADHero adHero = (ADHero) heros.get(1); ADHero adHero2 = (ADHero) heros.get(0); } }
The benefits of using generics :
The use of generics is to add... After the container <Type>
Type It can be a class , Abstract classes , Interface
Generics represent such containers , Can only store APHero,ADHero You can't put it in .
package generic; import java.util.ArrayList; import charactor.APHero; public class TestGeneric { public static void main(String[] args) { ArrayList<APHero> heros = new ArrayList<APHero>(); // only APHero You can put it in heros.add(new APHero()); //ADHero I can't even put it in //heros.add(new ADHero()); // There is no need to transform when acquiring , Because it must be APHero APHero apHero = heros.get(0); } }
Suppose the generic type of the container is Hero, So Hero Subclasses of APHero,ADHero Can be put in
and Hero Irrelevant types Item Still can't put it in
package generic; import java.util.ArrayList; import property.Item; import charactor.ADHero; import charactor.APHero; import charactor.Hero; public class TestGeneric { public static void main(String[] args) { ArrayList<Hero> heros = new ArrayList<Hero>(); // Only as Hero Subclasses of can be put in heros.add(new APHero()); heros.add(new ADHero()); // and Hero Irrelevant types Item Still can't put it in //heros.add(new Item()); } }
In order not to warn the compiler , You need to use generics before and after , like this :

ArrayList<Hero> heros = new ArrayList<Hero>();

however JDK7 Provides a generic shorthand that can slightly reduce the amount of code

ArrayList<Hero> heros2 = new ArrayList<>();

The following generics can be used <> Instead of , A little is better than nothing
package generic; import java.util.ArrayList; import charactor.Hero; public class TestGeneric { public static void main(String[] args) { ArrayList<Hero> heros = new ArrayList<Hero>(); // You can only use <> ArrayList<Hero> heros2 = new ArrayList<>(); } }
package generic;
 
import java.util.ArrayList;

import charactor.Hero;
 
public class TestGeneric {
 
    public static void main(String[] args) {
        ArrayList<Hero> heros = new ArrayList<Hero>();
        // You can only use <>
        ArrayList<Hero> heros2 = new ArrayList<>();
        
    }
}
Example 5 :

practice - Generic

edit top fracture   Wrong posture , get half the results with double the effort ! Click to see the correct posture for the exercise
According to Digital class Knowledge of , Design a collection , You can put integers in this set , You can also put floating point numbers , But you can't put strings
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Viewing this answer will cost 3 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Intermediate total 0 One answer ( Total required 0 Integral )
Viewing this answer will cost 3 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Intermediate total 0 One answer ( Total required 0 Integral )
Account not activated Account not activated , Limited functionality . Please click activate
This video is interpretive , So I hope you have read the content of this answer , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning

42 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


Generics use Number, You can put all Number Subclasses of , For example Integer,Float,Double

But you can't put String
package generic; import java.util.ArrayList; public class TestGeneric { public static void main(String[] args) { ArrayList<Number> numbers = new ArrayList<>(); numbers.add(123);//int numbers.add(123f);//float numbers.add(123d);//double numbers.add("String"); } }
package generic;
   
import java.util.ArrayList;
   
public class TestGeneric {
   
    public static void main(String[] args) {
        ArrayList<Number> numbers = new ArrayList<>();
        numbers.add(123);//int
        numbers.add(123f);//float
        numbers.add(123d);//double
        
        numbers.add("String");
        
    }
      
}


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-01-07 mogul We just came here to learn Please put F12 Unlock the shield This is just a simple front-end process You can still see A good solution is to make the video address dynamic
zeroowe

ddasda




3 One answer

mxchen
Answer time :2021-04-24
For those who understand , Check the codes that spend points and don't spend points , I know how to change

Yun sang
Answer time :2021-02-07
It means press... In the browser f12, Then change the front-end code to see the answer ( Don't spend points )

be stranded CN
Answer time :2021-01-19
What do you mean F12 Unlock the shield , I can press f12 Ah ..



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2021-01-07 The boss has to charge for the video, click it directly, and then give the video address , Don't just hide and find it No meaning
zeroowe

There is no alternative but to let the flowers fall




2 One answer

clytze097
Answer time :2021-04-09
I want to ask how to modify ?

einstein8
Answer time :2021-03-25
Well , The big man naturally knows , For those who want to learn, this meaning is not obvious , It's still broken



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-07-10 answer
2020-05-14 Why are subclass objects allowed to be added ?
2020-05-14 Why are subclass objects allowed to be added ?


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 4 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
about JAVA Intermediate - Generic - Generics in collections Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot