Movatterモバイル変換


[0]ホーム

URL:


Difference between Array vs ArrayList in Java

What is the difference between Array and ArrayListis quite a common question among beginners especially those who started coding inC andC++ and prefer to use Array? Both Array and Array Listare used to store elements, which can be either primitive or objects in case of Array and only objects in case ofArrayListin Java. The main difference between Array vs ArrayList in Java is the static nature of the Array and the dynamic nature of ArrayList. Once created you can not change the size of Array but ArrayListcan re-size itself when needed. 

Another notable difference betweenArrayListand Array is that Array is part of core Java programming and has special syntax and semantics support in Java, WhileArrayListis part of the Collection framework along with other popular classes like Vector,Hashtable,HashMap or LinkedList.

Let's see some more difference between Array andArrayListin Java in point form for better understanding.



Array vs ArrayList in Java

1) First and Major difference between Array andArrayListin Java is that Array is afixed-length data structure whileArrayListis a variable-lengthCollection class. You can not change the length of Array once created in Java butArrayListre-size itself when gets full depending upon the capacity and load factor. 

SinceArrayListis internally backed by Array in Java, any resize operation inArrayListwill slow down performance as it involves creating a new Array andcopying content from the old array to the new array.

2) Another difference between Array andArrayListin Java is that you can not useGenerics along with Array, as Array instance knows about what kind of type it can hold and throwsArrayStoreException if you try to store type which is not convertible into the type of Array.ArrayListallows you to use Generics to ensure type safety.



3) You can also compare Array vsArrayListonHow to calculate the length of Array or size of ArrayList. All kinds of Array provideslength variable which denotes the length of Array while ArrayListprovidessize() method to calculate the size ofArrayListin Java.

4) One more major difference betweenArrayListand Array is that,you can not store primitives in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java. ThoughAutoboxing of Java 5 may give you an impression of storing primitives inArrayList, it actually automatically converts primitives to Object. e.g.

ArrayList<Integer> integerList =newArrayList<Integer>();
integerList.add(1);//here we are not storing primitive in ArrayList, instead autoboxing will convert int primitive to Integer object


5) Java providesadd() method to insert an element intoArrayListand you can simply use the assignment operator to store element into Array e.g. In order to storeObject to specified position use

Object[] objArray =newObject[10];
objArray[1] =newObject();



6) One more difference on Array vsArrayListis that you can create an instance ofArrayListwithout specifying size, Java will create Array List with default size but it's mandatory to provide the size of Array while creating either directly or indirectly by initializing Array while creating it. By the way, you can alsoinitializeArrayListwhile creating it.

Difference between Array vs ArrayList in Java



Difference between Array and ArrayList in Java with exampleThat's all on the difference between Array andArrayListin Java. In terms of performance Array andArrayListprovides similar performance in terms of constant time for adding or getting element if you know index. 

Though automatic resize ofArrayListmay slow down insertion a bit Both Array andArrayListis the core concept of Java and any serious Java programmer must be familiar with these differences between Array andArrayListor in more general Array vs List.


OtherJavaArrayListTutorials you may find useful

20 comments:

  1. Very informative! Thumbs up!

    ReplyDelete
  2. Good Tutorial , keep it up
    thanks

    ReplyDelete
  3. Very good Tutorial,keep it up .
    Thank you

    ReplyDelete
  4. very very good tutorial

    ReplyDelete
  5. Very helpful and straight to the point. Thanks a bunch!

    ReplyDelete
  6. Very Helpful thanks

    ReplyDelete
  7. nice one dude..keep it up

    ReplyDelete
  8. Very good dude, thumbs up!

    ReplyDelete
  9. Make sense dude. Im Arjay.

    ReplyDelete
  10. thanks from the future :)

    ReplyDelete
  11. Thanks for sharing this once again..

    ReplyDelete
  12. I guess another difference is the way of selecting element. Array select element by [] and take O(1), ArrayList select by get(...) and take O(N)

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.


[8]ページ先頭

©2009-2025 Movatter.jp