Meet java-14.1 from the beginning again control arrays and containers

Source: Internet
Author: User

In this chapter we are again in-depth control over arrays and containers.

Arrays and containers are mainly concentrated in three areas: efficiency, type, underlying type.

We can make comparisons from three aspects.

1. Efficiency

Down here is the speed of add and get

The following is an example of add:

Package Com.ray.ch13;import Java.util.arraylist;public class Test {public static void main (string[] args) {Integer zero = New Integer (0); arraylist<integer> list = new arraylist<integer> (), Long startTime = System.currenttimemillis (); for (int i = 0 ; i < 1000000; i++) {list.add (zero);} Long endTime = System.currenttimemillis (); System.out.println (Endtime-starttime); integer[] integers = new Integer[1000000];starttime = System.currenttimemillis (); for (int i = 0; i < 1000000; i++) {integers[i] = zero;} EndTime = System.currenttimemillis (); System.out.println (Endtime-starttime); System.out.println ("----------------"); integer[] Integers2 = new Integer[1000000];starttime = System.currenttimemillis (); for (int i = 0; i < 1000000; i++) {integers2[i] = zero;} EndTime = System.currenttimemillis (); System.out.println (Endtime-starttime); arraylist<integer> list2 = new arraylist<integer> (); startTime = System.currenttimemillis (); for (int i = 0; I & Lt 1000000; i++) {List2.Add (zero);} EndTime = System.currenttimemillis (); System.out.println (Endtime-starttime);}}
Output:

26
2
----------------
2
29

From the results can be seen, is obviously an array of fast very much, but one thing to note is that we above the code to exclude two points, one is put into the object to be established first. Second, the container or array should be established first, simple control of the speed of the add, otherwise there will be deviations. I was here for 20 minutes to get around. To get the final result.


Efficiency is an array winner, but the array sacrifices the ability to expand. Despite the failure of the container, but it is the ability to expand capacity, is due to the need for this capacity to expand, sacrificing efficiency.

However, for common applications. With only dozens of or hundreds of objects, it is recommended that the container is completely negligible due to the loss of efficiency.


2. Type

In the preceding example, we have already taken the relevant sample, the array is type-safe and the container may be of unsafe type.

Examples:

Package Com.ray.ch13;import Java.util.arraylist;public class Test {public static void main (string[] args) {ArrayList list = new ArrayList (); List.add (1); List.add ("1"); integer[] integers = new Integer[10];integers[0] = 0;//integers[1]= "1";// Error}}

3. Basic type

The array supports the underlying type, which is not supported by the container.

Examples:

Package Com.ray.ch13;public class Test {public static void main (string[] args) {//Arraylist<int> list = new Arrayli St ();//errorint[] arr = new int[10];}}

4. In general

The author also recommends the use of containers. Since the Java container already has all the array operations, it also expands. This is very important for programming, which reduces the complexity of programming.

The hypothesis, of course, is the experimental stuff above. Another matter


Summary: We've compared arrays and containers in three ways. and illustrative examples.


This chapter is here, thank you.

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

Folder



Meet java-14.1 from the beginning again control arrays and containers

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.