Develop a data storage container using internal classes to store data containers

Source: Internet
Author: User

Develop a data storage container using internal classes to store data containers

Case study: a container is developed to store key-value pairs, English names, Chinese names, and internal classes;

Case Design:

① Use static internal classes to encapsulate key-value pairs;

② By default, the container size is 5, which is twice the size of the container if the container size exceeds 5;

③ Call the entryArrays method to return data in the container;

1 import java. util. arrays; 2 public class EntryDemo {3 public static void main (String [] args) {4 MyContainer container = new MyContainer (); 5 container. put ("jack", "jack"); 6 container. put ("jay", "jay Chou"); 7 container. put ("john", "john"); 8 container. put ("rose", "rose"); 9 container. put ("jack", "James"); 10 11 MyContainer. entry [] entrys = container. entryArrays (); 12 for (int I = 0; I <entrys. length; I ++) {13 MyContainer. entry entry = entrys [I]; 14 System. out. println (entry. getKey () + "--" + entry. getValue (); 15} 16} 17} 18 19 class MyContainer {20 // array for storing entry objects, default size: 521 private Entry [] entrys = new Entry [5]; 22 private int count = 0; 23 24 // provide an external interface to store encapsulated data in the container 25 public void put (String key, String value) {26 Entry entry = new Entry (); 27 entry. setKey (key); 28 entry. setValue (value); 29 entrys [count ++] = entry; // store the entry object to the array 30 // resize the Array 31 if (count> = entrys. length) {32 // the size of the new expanded array 33 int newCapacity = entrys. length * 2; 34 // copy the data in the old array to 35 entrys = Arrays in the new array with the length newCapacity. copyOf (entrys, newCapacity); 36} 37} 38 39 // return 40 public Entry [] entryArrays () {41 return Arrays. copyOfRange (entrys, 0, count); 42} 43 44 // encapsulate key-value pairs in the Entry object 45 public static class Entry {46 private String key; 47 private String value; 48 public void setKey (String key) {49 this. key = key; 50} 51 public String getKey () {52 return key; 53} 54 public void setValue (String value) {55 this. value = value; 56} 57 public String getValue () {58 return value; 59} 60} 61}

 

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.