In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Java List to add objects to the collection of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this article on how to use Java List to add objects to the collection will have a harvest, let's take a look at it.
1. Add ()
The easiest way to add an object to the Java collection is an add () method that the collection itself carries.
/ / create a student instance object Student stu1= new Student (1, "Xiaoming", 19, "male"); / / add objects to the collection students.add (stu1); / / check whether Student temp= (Student) students.get (0) has been added successfully System.out.println ("added" + temp.ID+ "student" + temp.name+ ", is a" + temp.age+ "year-old" + temp.gender+ "student!)
Print the results:
Added No. 1 student Xiaoming, is a 19-year-old boy!
Add (index,obj) can represent the object where the index will be located in the collection, using the new obj object, for example:
/ / overwrite the original location of the object Student stu2= new Student (2, "Xiao Hong", 18, "female"); students.add (0jue stu2); Student temp1= (Student) students.get (0); System.out.println ("added" + temp1.ID+ "student" + temp1.name+ ", is a" + temp1.age+ "age" + temp1.gender+ "student!)
Print the results:
Added No. 1 student Xiaoming, is a 19-year-old boy!
Add (index,obj) can represent the object where the index will be located in the collection, using the new obj object, for example:
/ / overwrite the original location of the object Student stu2= new Student (2, "Xiao Hong", 18, "female"); students.add (0jue stu2); Student temp1= (Student) students.get (0); System.out.println ("added" + temp1.ID+ "student" + temp1.name+ ", is a" + temp1.age+ "age" + temp1.gender+ "student!)
Print the results:
Added No. 2 student Xiao Hong, is an 18-year-old girl!
The index parameter of this method can add a location based on the existing set size, but cannot be crossed, otherwise an error will be reported, for example:
Students.add (2); 2. AddAll ()
If it is too troublesome to add objects one by one to the collection, the collection also provides an one-click operation, addAll (), you can add multiple objects to the collection at once.
/ / one-click added method Student [] stus= {new Student (3, "Xiaogang", 20, "male"), new Student (4, "Xiao Li", 19, "female")}; students.addAll (Arrays.asList (stus)); Student temp2= (Student) students.get (1); System.out.println ("added students" + temp2.name); Student temp3= (Student) students.get (2) System.out.println ("added students" + temp3.name)
The above is to first create an array of objects, then convert the array into a collection by the Arrays.asList method, and finally add it to the collection by the addAll method.
Print the results:
Added student Xiaoming
Added student Xiaogang
Like the add () method, you can overwrite the original object or add a new object in the next place, but you just can't add it across, otherwise an error will be reported.
Student [] stus1= {new Student (5, "Lamb", 12, "female"), new Student (6, "Pig", 15, "male")}; students.addAll (1, Arrays.asList (stus1)); Student temp4= (Student) students.get (1); System.out.println ("added students" + temp4.name); Student temp5= (Student) students.get (2) System.out.println ("added students" + temp5.name)
Print the results:
Added the student lamb.
Student piglet was added.
Here is to start from position 1 to add, that is, to cover the original Xiao Gang, Xiao Li two objects, replaced by lambs, piglets two objects.
This is the end of the article on "how to use Java List to add objects to a collection". Thank you for reading! I believe that everyone has a certain understanding of "how to use Java List to add objects to the collection" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.