In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the common methods of List in the foundation of Java". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the common methods of List in the foundation of Java?"
I. brief introduction of List interface
List is an ordered and repeatable set. It inherits the Collection interface and is an element that can be duplicated in the List collection, and the element at the specified location can be accessed through the index (subscript).
2. The common method of List-- void add (int index,Obejct element) method
The 1.void add (int index,Obejct element) method inserts the element element at the specified location, and the subsequent element moves back one element.
Example of the 2.void add (int index,Obejct element) method:
Import java.util.ArrayList; import java.util.List; public class p6 {public static void main (String [] args) {/ / TODO Auto-generated method stub List l = new ArrayList (); l.add ("Java"); l.add ("Database principles and applications"); l.add ("Software testing"); l.add ("Python practice") / l set all elements System.out.println ("l set all elements" + l); / / insert element l.add (1, "data structure") at the specified location; / / collect all elements System.out.println after element insertion ("all elements after element insertion" + l);}}
The result of the operation is:
Third, the common method of List-- boolean addAll (int index,Collection c) method
The 1.boolean addAll (int index,Collection c) method inserts all the elements of the collection c into the location specified by the List collection.
Example of the 2.boolean addAll (int index,Collection c) method:
Import java.util.ArrayList; public class p7 {public static void main (String [] args) {/ / TODO Auto-generated method stub ArrayList A1 = new ArrayList (); a1.add ("Java"); a1.add ("Database principles and applications"); a1.add ("C language"); System.out.println ("all elements of A1:" + A1); ArrayList a2 = new ArrayList () A2.add ("software testing"); a2.add ("Python practice"); a2.add ("Java EE"); System.out.println ("all elements of a2:" + a2); / / insert all elements of a2 in A1 specified location a1.addAll (2mema2) System.out.println ("insert all elements of A1 at the specified location of A1, all elements of A1 are:" + A1);}}
The result of the operation is:
4. The common method of List-- Object get (int index) method
The 1.Object get (int index) method returns the element at the specified location of the List collection.
Example of 2.Object get (int index) method
Import java.util.ArrayList; public class p8 {public static void main (String [] args) {/ / TODO Auto-generated method stub ArrayList a = new ArrayList (); a.add ("software testing"); a.add ("Java"); a.add ("Python combat"); a.add ("PHP+MYSQL") System.out.print ("element with index 3:" + a.get (3));}}
The result of the operation is:
5. The common method of List-- Object remove (int index) method
The 1.Object remove (int index) method deletes the element at the specified location.
Example of 2.Object remove (int index) method
Import java.util.ArrayList; public class p8 {public static void main (String [] args) {/ / TODO Auto-generated method stub ArrayList a = new ArrayList (); a.add ("software testing"); a.add ("Java"); a.add ("Python combat"); a.add ("PHP+MYSQL") System.out.println ("remove elements with index 3:" + a.remove (3)); System.out.println (all elements in "a:" + a);}}
The result of the operation is:
6. The common method of List-- int indexOf (Object o) method
The 1.int indexOf (Object o) method returns the index of the location where object o appears in the List collection, or-1 if no object o appears.
two。 The code is as follows:
Import java.util.ArrayList; public class p9 {public static void main (String [] args) {/ / TODO Auto-generated method stub ArrayList a = new ArrayList (); a.add ("software testing"); a.add ("Java"); a.add ("Python combat"); a.add ("PHP+MYSQL"); System.out.println ("all elements in an are:" + a) Int num=a.indexOf ("Python actual combat"); System.out.println ("first appearance\" Python actual combat\ "index value is:" + num); num=a.indexOf ("data structure"); System.out.println ("first occurrence\" data structure\ "index value is:" + num);}}
The result of the operation is:
Thank you for your reading, the above is the content of "what are the common methods of List in the foundation of Java". After the study of this article, I believe you have a deeper understanding of what the common methods of List in the foundation of Java are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.