MYTUTOR SUBJECT ANSWERS

244 views

What is the difference between an array and an ArrayList in Java?

An array is a way of storing information, which can vary from simple integers to strings and characters. In everyday life, we would probably call this a list. A traditional array of integers in java is defined like so:

int[] myArray = new int[3];

Here we have declared an integer array of size 3. The size of an array in Java is immutable; this means that myArray can only ever hold 3 items. If I then wanted to add the number 3 in the first position, I would simply do:

myArray[0] = 3;

If we then deleted 3 from our array, the size of the array would stay the same. We are simply deleting the value at position 0, not the position itself. 

An ArrayList is Java's answer to "mutable" arrays. We don't need to define a length when we instantiate them, and we can add as many elements as we want: the size of the array grows or shrinks. We declare an ArrayList of strings like so:

ArrayList myArrayList = new ArrayList();

We then have several methods, such as add(), remove(), or size(), which allow us to manipulate the ArrayList. To get an element from the array at a certain position, we use its get() method:

myArrayList.get(index);

Overall, we would use simple arrays when we have a pre-defined length for our array, and we know that the number of items we want to add won't surpass this length. ArrayLists are ways of getting around that, and therefore allow more flexibility. One last difference to notice between these two data types: ArrayList cannot store primitive data types. ArrayLists can only store objects, such as strings. Since Java 5, autoboxing was introduced which allowed you to add primitve types, such as integers, to ArrayLists; but they are in fact converting them into objects. So think carefully about your problem before you choose which version of the array you would like to use!

Jonathan W. A Level Computing tutor, A Level French tutor, GCSE Maths...

9 months ago

Answered by Jonathan, an A Level Computing tutor with MyTutor

Still stuck? Get one-to-one help from a personally interviewed subject specialist

3 SUBJECT SPECIALISTS

£20 /hr

Christopher P.

Degree: Computer Science (Bachelors) - Warwick University

Subjects offered: Computing, Maths+ 1 more

Computing
Maths
History

“About me: I am a Computer Science student currently studying at Warwick University. I've always had a passion throughout my education for Maths and Computing, as the vast array of ideas that you can bring to life through programming i...”

MyTutor guarantee

£20 /hr

Kyle C.

Degree: Computer Science With Electronics (Bachelors) - Edinburgh University

Subjects offered: Computing, Physics+ 1 more

Computing
Physics
Maths

“Who am I?I am a student pursuing a Computer Science degree at the University of Edinburgh. I enjoy working on theatre lighting and am also a keen fencer which is where I initially started teaching by coaching younger fencers.Sessio...”

£20 /hr

Michael T.

Degree: MMath (Masters) - Durham University

Subjects offered: Computing, Physics+ 1 more

Computing
Physics
Maths

“About Me: I am currently studying Mathematics at Durham University. Maths can sometimes be intimidating, but it's that which makes it so rewarding when it all starts to make sense. I have been teaching in one form or another since 13...”

About the author

Jonathan W.

Currently unavailable: for regular students

Degree: Computer Science and Artificial Intelligence (Bachelors) - Birmingham University

Subjects offered: Computing, Maths+ 1 more

Computing
Maths
French

“About Me: I am a Computer Science And Artificial Intelligence student at the University of Birmingham. I am really in love with my subject- which involves a lot of maths, as well as computing- and Ienjoy teaching other people about th...”

You may also like...

Posts by Jonathan

How do we solve simultaneous equations, say for example x + 4y = 20 and 2x - 2y = 10 ?

What is the difference between an array and an ArrayList in Java?

When should we use the imperfect tense or the simple past tense?

Other A Level Computing questions

What do the logical operators AND, OR, XOR and NOT do?

What is the difference between local and global variables?

What is an OOP (Object Oriented Programming) language?

How does a HDD (Hard drive disk) work?

View A Level Computing tutors

Cookies:

We use cookies to improve our service. By continuing to use this website, we'll assume that you're OK with this. Dismiss

mtw:mercury1:status:ok