Java Programming Idea (second edition) No. 09: Hold Your Object

Source: Internet
Author: User
Tags array functions modify
Programming | objects
This is a very simple plan: a fixed number of objects with known lifespan (objects).





You have to be able to produce any number of objects at any point in time. To do this, Java provides several ways to hold objects (or reference of objects). There is a built-in array, as well as a complete set of container classes in utilities or a cluster class (collection classes).





The difference between array and other containers: efficiency and type.

If you can, you should use array as much as possible. Of course sometimes he has limited functions, so he needs other containers.

Java Array and other containers can cause additional efficiency burdens due to boundary checks.

Other containers treat holding objects as object types.

Basic type (primitive types) cannot be directly placed in a normal container?





Various initialization actions for array:





Java's referral mechanism makes it very easy to return an array.

Arrays class has a set of static functions that can perform many array common functions: Equals (), fill (), sort (), BinarySearch ()





When you don't know exactly how many objects you need, you need more complex ways to hold them. Java provides a set of container class libraries to solve this problem. Basic types include: List,set,map can use them to solve very many problems.

They can place any number of objects, and when composing an application, there is no need to worry about capacity issues:

They can basically be divided into two basic categories: Collection,map.





Disadvantages of the container: element type is undetermined

This is because the Java container is an object of its element type. On the bad side, there are some problems with this design: 1. You plan to design some containers that can only store the cat, but the user can keep the dog in. 2. When you take out the time do not know whether it is a cat or a dog, need to transition downcasting.

Let's think about the good: Java is more than just the objects in your container. We also have the execution period anomaly (runtime exception), Wahaha.

tips:jdk1.4 has adopted "Generic Java" technology to become a veritable "generics Java". Allows programmers to specify element types when using Java containers. Syntax is very close to C + +, only the underlying implementation of the technology "completely different."





Iterator (iterators)

Everything is for code reuse, and if there are no iterators, we may have to modify a lot of the traversal code when we modify the container. With the Iterators object, we can reuse it.

Its job is to visit and select a series of objects in the sequence (sequence). The client programmer does not need to know or care about how the sequence is implemented at the bottom. They are "Lightware" objects: The costs are small. For this reason, you often find that he has some seemingly bizarre limitations, such as some iterators that can only be moved in one direction.





Faint, too much, and then look at it with the fall. J






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.