Self-made data structures (containers)-java development with the most ArrayList and HashMap

Source: Internet
Author: User




public class Myarraylist<e> {private int capacity = 10; private int size = 0; Private e[] values = NULL; @SuppressWarnings ("unchecked") public myarraylist () {values = (e[]) new object[capacity]; } @SuppressWarnings ("Unchecked") public myarraylist (int capacity) {this.capacity = capacity; Values = (e[]) new object[this.capacity]; } public void put (E e) {if (E = = null) {throw new RuntimeException ("The value should not is Null. ");} if (size >= capacity) {enlargecapacity (); } Values[size] = e; size++; Public E get (int index) {if (index >= size) {throw new RuntimeException ("The Index:" + Index + "is out of band."); } return Values[index]; } public void Remove (int index) {if (index >= size) {throw new RuntimeException ("The index: "+ index +" is out of band. "); for (int i = index; i < size-1; i++) {Values[i] = values[i + 1]; } values[size-1] = null; size--; } @SuppressWarnings ("Unchecked") private void enlargecapacity () {capacity = capacity * 2; E[] Tmpvalues = (e[]) new object[capacity]; System.arraycopy (values, 0, tmpvalues, 0, size); values = tmpvalues; The public String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("["); for (int i = 0; i < size; i++) {sb.append (Values[i]). Append (","); } if (Size > 0) {sb.deletecharat (Sb.length ()-1); } sb.append ("]"); return sb.tostring (); }/** * @param args */public static void main (string[] args) {myarraylist<integer> MyList = new myarraylist<integer> (); for (int i=0;i<1000;i++) {mylist.put (i); } System.out.println (Mylist.tostring ()); } }

  

Self-made data structures (containers)-java development with the most ArrayList and HashMap

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.