Get to know java-14.1 from the ground up and compare arrays and containers again

Source: Internet
Author: User

Get to know java-14.1 from the ground up and compare arrays and containers again

This chapter further compares arrays and containers.

Arrays and containers are mainly concentrated in three aspects: efficiency, type, and basic type. We can make a comparison from three aspects.

1. Efficiency

Here, it refers to the speed of adding and get.

The following uses add as an example:

 

package com.ray.ch13;import java.util.ArrayList;public class Test {public static void main(String[] args) {Integer zero = new Integer(0);ArrayList
 
   list = new ArrayList
  
   ();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
   
     list2 = new ArrayList
    
     ();startTime = System.currentTimeMillis();for (int i = 0; i < 1000000; i++) {list2.add(zero);}endTime = System.currentTimeMillis();System.out.println(endTime - startTime);}}
    
   
  
 
Output:

 

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

From the results, we can see that the array is much faster, but one thing to note is that the above Code should exclude two points. First, we must first establish the object to be put in, second, the container or array should also be set up first, simply compare the add speed, otherwise there will be deviations. I spared 20 minutes to get the final result.

 

The efficiency is that the array wins, but the array sacrifices the ability to expand. Although the container fails, what it gets is the ability to expand, because the expansion capability is required to sacrifice the efficiency.

However, for common applications that only have dozens or hundreds of objects, we recommend containers because the efficiency loss is negligible.

 

2. Type

As we have mentioned above, arrays are type-safe and containers may be type-insecure.

Example:

 

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 Types

 

Arrays support basic types, but containers do not.

Example:

 

package com.ray.ch13;public class Test {public static void main(String[] args) {// ArrayList
 
   list = new ArrayList();//errorint[] arr = new int[10];}}
 

4. In general

I recommend using containers because java containers already have all the operations on arrays and make extensions. This is very important for programming and reduces programming complexity.

Of course, if it is an experimental thing above, let alone.

Summary: we have compared arrays and containers from three aspects and provided examples.

This chapter is here. Thank you.

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.