1 Object Array
PackageCom.xuweiwei;/*** Student Class*/ Public classStudent {PrivateString name; PrivateInteger age; PublicStudent () {} PublicStudent (String name, Integer age) { This. Name =name; This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @Override PublicString toString () {return"student{" + "name=" + name + ' \ ' + ", age=" + Age + '} '; }}
PackageCom.xuweiwei;/*** I have 5 students, please store the information of these 5 students in an array, and iterate through the array to get information about each student*/ Public classObjectarraydemo { Public Static voidMain (string[] args) {Student S1=NewStudent ("A", 8); Student S2=NewStudent ("B", 9); Student S3=NewStudent ("C", 10); Student S4=NewStudent ("D", 11); Student S5=NewStudent ("E", 12); Student[] Students={S1,S2,S3,S4,S5}; for(inti = 0; i < students.length; i++) {Student s=Students[i]; System.out.println (s); } }}
2 Overview of Integrated 2.1 2.1.1 Why does the collection appear?
- Object-oriented language is the embodiment of things in the form of objects, so in order to facilitate the operation of multiple objects, Java provides a collection of classes.
What is the difference between a 2.1.2 array and a collection class in the same container?
- Arrays can also store objects, but the lengths are fixed, and the length of the collection can vary.
- The base type of data can be stored in an array, but the collection can only store objects.
Characteristics of 2.1.3 Collection class
- The ① collection can store only objects.
- The length of the ② collection can vary.
- The ③ collection can store different types of objects.
Java Fundamentals 14