Java Collection Framework--arraylist classes, vectors, and LinkedList classes

Source: Internet
Author: User

1.ArrayList class overview

The underlying data structure is an array, query block, and delete slowly.

Threads are unsafe and highly efficient.


2.ArrayList case

2.1 Storing strings and traversing

package com;import java.util.arraylist;import java.util.iterator;import  Java.util.listiterator;public class arraylistdemo {public static void main ( String[] args)  {//Create ArrayList object arraylist<string> list = new arraylist< String> ();//add Element List.add ("haha"); List.add ("hehe"); List.add ("Hee-hoo"); List.add ("stupid");//traversal mode one   Replace the ArrayList collection with the Object[]object[] obj = list.toarray ();for  (int i = 0; i  < obj.length; i++)  {String str =  (String)  obj[i]; System.out.println (str);} System.out.println ("----------------------");//Traverse mode two   use Iterator iterator for (iterator<string>  Iterator = list.iterator (); Iterator.hasnext ();) {string str = iterator.next (); System.out.println (str);} System.out.println ("-----------------------");//Traverse Mode III   use Listiterator iterator//forward traverse listiterator<string>  listiterator = list.Listiterator (); while (Listiterator.hasnext ()) {string str = listiterator.next (); System.out.println (str);} System.out.println ("------------------------");//Reverse Traversal while (listiterator.hasprevious ()) {string str =  listiterator.previous (); System.out.println (str);} System.out.println ("------------------------");//Traverse mode four   use the size () and get (Int index) methods to implement traversal of the collection element for  ( Int i = 0; i < list.size ();  i++)  {String str =  List.get (i); System.out.println (str);}}}

2.2 Store custom objects and traverse

Omitted


3.Vector class

Package com;import java.util.enumeration;import java.util.vector;/** * vector Class-specific features  * 1. Adding features  * public void addelement (e e)  *       2. Get Features  *          public E  ElementAt (Int index)  *      public enumeration elements ()   * */public class vectordemo {public static void main (String[]  args)  {//Create collection Object vector<string> v = new vector<string> ();// add element v.addelement ("Hello"); V.addelement ("World"); V.addelement ("Java");//Traverse for  (int i = 0;  i < v.size ();  i++)  {string str = v.elementat (i); System.out.println (str);} System.out.println ("------------------");for  (enumeration<string> enumeration =  V.elements ();  enumeration.hasmoreElements (); )  {string str = enumeration.nextelement (); System.out.println (str);}}}

Hello

World

Java

------------------

Hello

World

Java


4.LinkedList class overview

The underlying data structure is linked list, query slow, delete quickly.

Threads are unsafe and highly efficient.


Unique features of the 5.LinkedList

Package com;import java.util.linkedlist;/** * linkedlist features  *  add-on features  *       public void addfirst (e e )  *       public void addlast (e e)  *       get features   *      public e getfirst ()  *       Public e getlast ()  *       Delete features  *       public e removefirst ()  *      public E  Removelast ()   */public class linkedlistdemo {public static void main ( String[] args)  {//Create a Collection object linkedlist<string> list = new linkedlist<string > ();//add Element List.add ("Hello"); List.add ("World"); List.add ("Java"); SYSTEM.OUT.PRINTLN (list);//[hello, world, java]list.addfirst ("Java ee "); SYSTEM.OUT.PRINTLN (list);//[javaee, hello, world, java]list.addlast ("Android"); SYSTEM.OUT.PRINTLN (list);//[javaee, hello, world, java, android]system.out.println ( List.getfirst ());//javaeesystem.out.println (List.getlast ());//androidsystem.out.println (List.removeFirst ());// JAVAEESYSTEM.OUT.PRINTLN (list);//[hello, world, java, android]system.out.println ( List.removelast ());//androidsystem.out.println (list);//[hello, world, java]}}


Sleepy rest.








This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1862222

Java Collection Framework--arraylist classes, vectors, and LinkedList classes

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.