List of Java Basics

Source: Internet
Author: User

Java divides a container into storage of key-value pairs and stores ordinary objects, and map is used to store key-value pairs, and the subclasses of the collection interface are used to store ordinary objects. The set element is not repeatable, the underlying implementation is a hash table, the list of elements can be duplicated, logically, is a linear table, LinkedList is implemented with a linked list, and ArrayList is implemented with an array. The difference in function is this, then according to the specific business logic to choose it! Basic methods:Add elements: Add (Element), add (index,element) get element: Get (Index) modify element: Set (index, Element)Remove elements: Remove (Element), remove (index)Clear list: Clear ()judgment includes: Contains (Element)judgment null: IsEmpty ()List Length: size ()convert to array: ToArray ()
       //constructs an empty ArrayList that specifies the type of string data that is storedarraylist<string> list =NewArraylist<string>(); //A collection in Java cannot add a base data type, it must be a reference data type, or you can use a wrapper class if you want to add a base data typeList.add ("AAA"); //can increase the number of duplicate elementsList.add ("AAA"); List.add ("BBB"); //Add () is the method of inheriting collection; Add (index,element) is the List methodList.add (2, "CCC"); //Print the entire ArrayListSystem.out.println (list); //traverse individual elements and print         for(inti=0; I<list.size (); i++) {System.out.print (List.get (i)+ " "); } System.out.println ("\r\n====================================");
Output:[AAA, AAA, CCC, BBB]AAA AAA CCC BBB====================================
// Modify the element        of the position by index List.set (0, "AA");        System.out.println ("After reset, the list is:" +list);        System.out.println ("====================================");

Output

After reset, the list is: [AA, AAA, CCC, BBB]
====================================

        //from ArrayList to Array, the converted data type is Object        object[] Array = list.toarray ();          for (Object elem:array) {            + "");        }        System.out.println ("\r\nthe lenght of the array is:" + array.length);        System.out.println ("====================================");

Output

AA AAA CCC BBB
The lenght of the array is:4
====================================

    // Delete the element, you can pass in the subscript, or you can pass in the element        String  removed = list.remove (0);        System.out.println ("The removed element is:" +removed);        SYSTEM.OUT.PRINTLN (list);         boolean removed2 = List.remove ("AAA");        System.out.println (REMOVED2);        SYSTEM.OUT.PRINTLN (list);        System.out.println ("====================================");
Output:the removed element is:aa[aaa, CCC, BBB]TRUE[CCC, bbb]====================================
// Determines whether a collection contains an element        SYSTEM.OUT.PRINTLN ("list contains AAA:" +list.contains ("AAA"));         // determines whether the collection is empty        SYSTEM.OUT.PRINTLN ("List is empty?:" +List.isEmpty ());         // get the first element        System.out.println ("The fisrt element is:" + list.get (0));         System.out.println ("====================================");
Outputlist contains AAA?: Falselist is empty?: falseThe fisrt element IS:CCC====================================
// empty the entire collection         list.clear ();        System.out.println ("Afer clear, the list is:" +list);        System.out.println ("====================================");
Output:afer clear, the list is: []====================================
 //  iterate over the collection elements: Method 1  for  (int  i=0; i<list.size (); I++) {    System.out.print (List.get (i)  + "" );  //  iterate over the collection elements: Method 2  for   +" );  //  iterate over the collection elements: Method 3  iterator<    String> iterator = List.iterator ();  while   (Iterator.hasnext ()) {String El 
     = Iterator.next ();    System.out.println (EL); }

List of Java Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.