Java underlying implementation container, java underlying container

Source: Internet
Author: User

Java underlying implementation container, java underlying container

1 package StudyCollection; 2 3/** 4 * underlying implementation of simple containers 5*6 * @ author ouyang-an Thank you, Mr. Gao Qi from Shang xuexiang, 7 */8 9 public class MakeArray {10 private Object [] elementData; 11 private int size; 12 13 // No parameter constructor 14 public MakeArray () {15 this (10 ); // The default size is 1016} 17 18. // 19 public MakeArray (int initialCapacity) {20 if (initialCapacity <0) {21 try {22 throw new Exception ("the array is out of bounds! "); 23} catch (Exception e) {24 e. printStackTrace (); 25} 26} 27 elementData = new Object [initialCapacity]; 28} 29 30 // add () method 31 public void add (Object obj) {32 if (size = elementData. length) {33 Object [] newArray = new Object [size * 2]; 34 System. arraycopy (elementData, 0, newArray, 0,35 elementData. length); 36 elementData = newArray; 37} 38 elementData [size] = obj; 39 size ++; 40} 41 42 // size Method 43 public int size () {44 return size; 45} 46 47 // determines whether the array is null. 48 public boolean isEmpty () {49 return size = 0; 50} 51 52 // traverse 53 public void iteratesMethod () {54 for (int I = 0; I <size; I ++) {55 Object array_element = elementData [I]; 56 System. out. println (I + ":" + array_element); 57} 58} 59 60 // get Method 61 public Object get (int index) {62 if (index> = size) {63 try {64 throw new Exception (); 65} catch (Exception e) {66 e. printStackTrace (); 67} 68} 69 return elementData [index]; 70} 71 72 // set method 73 public Object set (int index, Object nObject) {74 return elementData [index] = nObject; 75} 76 77 public static void main (String [] args) {78 MakeArray list = new MakeArray (); 79 list. add ("11"); 80 list. add ("22"); 81 list. add ("33"); 82 list. add ("44"); 83 list. iteratesMethod (); 84 System. out. println ("------------------------"); 85 System. out. println (list. size); 86 System. out. println ("------------------------"); 87 System. out. println (list. isEmpty (); 88 System. out. println ("------------------------"); 89 System. out. println (list. get (3); 90 System. out. println ("------------------------"); 91 System. out. println (list. set (2, "ouyangan"); 92 System. out. println ("------------------------"); 93 list. iteratesMethod (); 94} 95}

 

Related Article

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.