Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java List to add objects to a collection

Shulou Source: shulou.com Published: 2022-06-01 13:18:02 10月03日 Update

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.

Tags: Object student method result location Xiaoming Xiaogang array knowledge Xiao Hong lamb two content in situ that is boy article one button will report Xiao Li Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno NVidia Shulou Information Apple