Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Database /数组和数组列表之间的区别

数组和数组列表之间的区别

June 27, 2011Posted byIndika

Arrays vs Arraylists

Arrays are the most commonly used data structure to store a collection of elements. Most programming languages provide methods to easily declare arrays and access elements in the arrays. An arraylist can be seen as a dynamic array, which can grow in size. Due to this reason, the programmer does not need to know the size of the arraylist when she is defining it.

What are Arrays?

Shown in figure 1, is a piece of code typically used to declare and assign values to an array. Figure 2 depicts how an array would look like in the memory.

int values[5];

values[0]=100;

values[1]=101;

values[2]=102;

values[3]=103;

值[4]= 104;

Figure 1: Code for declaring and assigning values to an array


100 101 102 103 104
Index: 0 1 2 3 4

图2:数组存储在memory

Above code, defines an array that can store 5 integers and they are accessed using indices 0 to 4. One important property of an array is that, entire array is allocated as a single block of memory and each element gets its own space in the array. Once an array is defined, its size is fixed. So if you are not sure about the size of the array at compile time, you would have to define a large enough array to be in the safe side. But, most of the times, we are actually going to use less number of elements than we have allocated. So a considerable amount of memory is actually wasted. On the other hand if the “large enough array” is not actually large enough, the program would crash.

What are Arraylists?

An arraylist can be seen as a dynamic array, which can grow in size. Therefore arraylists are ideal to be used in situation in which you don’t know the size of the elements required at the time of declaration. In Java, arraylists can only hold objects, they cannot hold primitive types directly (you can put the primitive types inside an object or use the wrapper classes of the primitive types). Generally arraylists are provided with methods to perform insertion, deletion and searching. Time complexity of accessing an element is o(1), while insertion and deletion has a time complexity of o(n). In Java, arraylists can be traversed using foreach loops, iterators or simply using the indexes.

What is the difference between Arrays and Arraylists

Even though the arrays and arraylists are similar in the sense that both of them are used to store collections of elements, they differ in how they are defined. The size of the array has to be given when an array is defined, but you can define an arraylist without knowing the actual size. You can add elements to an arraylist after it is defined and this is not possible with arrays. But in Java, arraylists cannot hold primitive types, but arrays can be used to hold primitive types. But if you need a data structure that can vary its size, arraylist would be the best choice.

Related posts:

Difference Between Arrays and Linked Lists Difference Between Linear and Nonlinear Data StructuresDifference Between Linear and Nonlinear Data Structures 堆栈和堆之间的区别 Difference Between DBMS and File System Difference Between Distributed Database and Centralized Database

Filed Under:DatabaseTagged With:array size,arraylist,arraylists in Java,arrays,data structure,dynamic array

About the Author:Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Comments

  1. Susansays

    January 24, 2019 at 8:36 pm

    so, can the arraylist hold objects of different types? (i.e) array is defined as integer array then it can hold all integers. but an arraylist can hold objects of different data types?

    Reply

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Atomic Number and Atomic Weight

Difference Between Implosion and Explosion

Difference Between Rate and Ratio

Difference Between Modelling and Simulation

Difference Between Capital Reserves and Revenue Reserves

Latest Posts

  • What is the Difference Between Prevention and Cure
  • What is the Difference Between DSM IV and DSM V Autism
  • What is the Difference Between Molecular Formula and Structural Formula
  • What is the Difference Between Acne and Eczema
  • What is the Difference Between Mutation Rate and Substitution Rate
  • What is the Difference Between Vermicompost and Compost
  • Home
  • Vacancies
  • About
  • Request Article
  • 联系我们

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.