Implementation of the Java container to list

Source: Internet
Author: User

Import java.util.arrays;public class collection_4_23_01 {public static void  main (String[] args)  {/* *  container  *  */arraylist_py arraylist_py1  = new arraylist_py (); Arraylist_py1.add ("Ppyy"); Arraylist_py1.add ("Ooii"); Arraylist_py1.add ("Xxqq"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.add ("Qqxx"); Arraylist_py1.showit (); System.out.println ("The length of the array is  " +arraylist_py1.getsize ()); System.out.println ("The second element is  " +arraylist_py1.getelement (1));}} interface list {//add Element void add  (object element);//delete element void remove  (int &NBSP;IDX);//Get length int getsize  ();//Get Element object getelement (INT&NBSP;IDX);} class arraylist_py implements list {private object[] datas;//an array of stored data private int arrlength;// Array length private int size;//number of elements {Datas = new object[10];arrlength = 10;size  = 0;} public void add  (object element)  {this.datas[size] = element;// Put the data in the array this.size++;//the number of elements plus a if  (this.size >= this.arrlength)  {object[] datas2  = new Object[arrLength+10]; System.arraycopy (datas, 0, datas2, 0, datas.length); this.datas = datas2;//address Substitution}} public void remove  (INT&NBSP;IDX)  {system.arraycopy (This.datas,idx+1,this.datas,idx, this.datas.length-(idx+1)); this.size--;} public int getsize  ()  {return this.size;} Public object getelement (INT&NBSP;IDX)  {if  (idx < 0 | |  idx > this.size-1)  {throw new arrayindexoutofboundsexception ("There is a problem with the incoming location, the element cannot be found ");}RETURN&NBSP;THIS.DATAS[IDX];} public void showit  ()  {for  (int i = 0; i < this.size ;  i++)  {if  (this.datas[i] != null)  {system.out.print ("[ " +this.datas[i]+ "  ]  ");}} System.out.println ("");}}


This article is from the "shadow debut" blog, please be sure to keep this source http://woodywoodpecker.blog.51cto.com/4820467/1637759

Implementation of the Java container to list

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.