Go Java Object Container

Source: Internet
Author: User

Use Java to implement Notepad functionality. Start by listing the features that Notepad requires:

    1. You can add records (strings);
    2. The number of record bars can be obtained;
    3. One of the records can be deleted;
    4. A record of the specified section may be obtained;
    5. You can list all the records.

If this notepad is part of a large program, that is, a top-level program, then the upper-level program may invoke one of the data listed above in this Notepad.

So we call the above listed functions as the interface of this notepad.

Then calling these interfaces is the public function (method) of this class through Notepad.

But how do you make a record? It is clear that the recorded string cannot be recorded in an array because the length of the array is pre-set. This is a generic container arraylist<> , this Arraylist is also a class of the system, so when using it to define a new object to come out: private arraylist<string>  Notes = new arraylist<string> (); also declare import java.util.ArrayList;

ArrayList can be arbitrarily stored in the data, unlimited number, which realizes the requirements of Notepad.

Basic operations for Arraylist: arraylist<string> notes

    • Notes.add ()
    • Notes.size ()
    • Notes.remove (Index)
    • Notes.get (Index)
    • Notes.toarray (string[] a=new string[notes.size ()])

The interface function of Notepad is implemented by the above operation.

1PackageNotebook23ImportJava.util.ArrayList;45PublicClassNotebook {67Private arraylist<string> notes =New arraylist<string>();89PublicvoidAdd (String s) {10Notes.add (s);11}1213PublicIntGetSize () {14ReturnNotes.size ();15}1617Publicvoid Removenote (IntIndex) {18Notes.remove (index);19}2021stPublic String Getnote (IntIndex) {22ReturnNotes.get (index);23}2425PublicString[] List () {String[] A =NewString[notes.size ()];27Notes.toarray (a);28ReturnA29}3031PublicStaticvoid Main (string[] args) {//TestNotebook NB =new Notebook ();  Nb.add ("frist"  System.out.println (Nb.getsize ());  String[] A = Nb.list () for38 Span style= "color: #000000;" > System.out.println (s); 39 }40 }41 42}       
Notebook.java

Run:

-----------------------------------------------------------------------------------------

In addition, the container type also has a collection container (set), such as HashSet, is also a class, has the attribute is the inner element is unordered, cannot have the duplicate element, and the Mathematics collection concept is identical.

You can see the difference between the two containers, ArrayList and HashSet, from the results of the program running.

Note: The program can also see that the output of two containers is no longer assigning each element of the container to another array, and then outputting each element in the array through a For Each loop. Here we are directly println out of the object of a container, is possible. This is because: {

As shown in the first red box, if there is a "publicString toString () {}" function in a class, you can directly println the object name of the class, and the output will automatically call the ToString function, as shown in the Second red box. So, we suspect that there must also be a function like "public String toString () {}" in the two common source files of ArrayList and HashSet .

}

-----------------------------------------------------------------------------------------

hashmap Container: hashmap<key,value>

A key corresponding to a value, when given a key multiple put, the key corresponds to the last put value, (one input denomination, output multi-dollar name of the program, such as: 1 cents is called 1penny. )

Traversal of HashMap:

Go Java Object Container

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.