Java basics-object container, java-object container

Source: Internet
Author: User

Java basics-object container, java-object container

Directory:

  • ArrayList sequential generic container
  • HashSet collection container
  • HashMap <Key, Value> container

 

 

Use Java to implement the notepad function. First, list the functions required by notepad:

If this notepad is a part of a large program, that is, there are upper-layer programs, the upper-Layer Program may call a data listed above this notepad.

Therefore, we call the functions listed above asInterface.

To call these interfaces, use the public function (method) of the class notepad ).

 

But how can we implement records? Obviously, the recorded string cannot be recorded in an array, because the length of the array is preset. In this case, we need to useWildcard container Arraylist <>, This arraylist is also a system class, so when using it, you need to define a new object: private Arraylist <String> notes = new Arraylist <String> (); also declare import java. util. arrayList;

Arraylist can store any data in it without limit, which meets the requirements of notepad.

Basic arraylist operations: Arraylist <String> notes

  • Notes. add ()
  • Notes. size ()
  • Notes. remove (index)
  • Notes. get (index)
  • Notes. toArray (String [] a = new String [notes. size ()])

Use the above operations to implement notepad interface functions.

 

1 package notebook; 2 3 import java. util. arrayList; 4 5 public class Notebook {6 7 private ArrayList <String> notes = new ArrayList <String> (); 8 9 public void add (String s) {10 notes. add (s); 11} 12 13 public int getSize () {14 return notes. size (); 15} 16 17 public void removeNote (int index) {18 notes. remove (index); 19} 20 21 public String getNote (int index) {22 return notes. get (index); 23} 24 25 public String [] list () {26 String [] a = new String [notes. size ()]; 27 notes. toArray (a); 28 return a; 29} 30 31 public static void main (String [] args) {// test32 Notebook nb = new Notebook (); 33 nb. add ("frist"); 34 nb. add ("second"); 35 System. out. println (nb. getSize (); 36 String [] a = nb. list (); 37 for (String s: a) {38 System. out. println (s); 39} 40} 41 42}Notebook. java

 Run:

 

 

Bytes -----------------------------------------------------------------------------------------

In addition, the container type also hasSet), SuchHashSetIt is also a class, which has the characteristics that internal elements are not sorted and cannot have repeated elements. It is the same as the Set Concept in mathematics.

The program running result shows the differences between the ArrayList and HashSet containers.

Note:The program can also see that the output of the two containers no longer assigns each element of the container to another array, and then outputs each element of the array through the for each loop. In this case, println directly generates a container object, which is acceptable. This is because :{

As shown in the first red box, if a class contains"Public String toString (){}"Function, the Object Name of the println class can be directly called, and the toString function is automatically called during output, as shown in the second red box.Therefore, we guess that the two public class source files, ArrayList and HashSet, must also have functions similar to "public String toString () {}"..

}

Bytes -----------------------------------------------------------------------------------------

HashMap container:HashMap <Key, Value>


A key corresponds to a value. After a key is put multiple times, the key corresponds to the value of the last put. (For an input denomination, a program that corresponds to a dollar name is output, for example: 1 cent is 1 penny .)

 

HashMap traversal:

 

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.