The biggest difference is that you can add to and remove from a list, whereas arrays have a fixed size. Answer from sepp2k on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ list vs array vs arraylist
r/javahelp on Reddit: List vs Array vs ArrayList
September 12, 2023 -

Coming from Python, this whole Java thing is incredibly confusing. The thing is, they don't seem to mix and uses different methods such as .size() vs .length(). All of them also seem to be doing the same thing: having a group of something.

I really need a comparison of the three, and how to declare, manipulate, and access, etc them.

Top answer
1 of 5
16
These are three different things: Array - a very basic data structure. It has a fixed, predetermined size that must be known at the time of creating (instantiating) the array. Think of it as a sorting box with several slots. Here, you have .length - without parentheses as it is a property, not a method List - an interface (not a class) that defines certain behavior. It cannot be instantiated on its own. ArrayList - a concrete class that implements the List interface. When you see a declaration like List data = new ArrayList<>(); there are several things going on: Listprogramming against the interface - here, the interface List is used as data type - this is good practice as it allows you to at any time change the internal representation of the List - e.g. from ArrayList to LinkedList if you figure out that the other better suits your needs. this is a data type identifier. It limits the type of elements that can be stored in the list. Here, only String objects are allowed. If you were to omit this, the list would store Object (the ancestor of all Java classes) and descendant instances (i.e. Object and any subclass of it). data - just the variable name new - the keyword to create a new Object Instance ArrayList<>() - the constructor call. Here, the constructor without parameters of the ArrayList class is called. The diamond operator <> is used to infer (take) the data type for the ArrayList from the variable type (here String). So, the whole is: we create a new variable of type List that can only accept String elements and ist internally represented as ArrayList.
2 of 5
4
Array An array is a data structure built into the Java language which can hold a number of elements, all of the same type. It cannot be resized once it has been created, and you cannot add elements to it without specifying an index. // creates an array of 10 ints int[] arr = new int[10]; // sets the value of the 0th element to 5 arr[0] = 5; // prints the value of the 0th element, which is 5 System.out.println(arr[0]); // Sets e to the value of the 0th element of arr, which is 5 int e = arr[0]; // Sets the value of the 1st element to 6 arr[1] = 6; // prints the length of arr System.out.println(arr.length); // even though arr[9] has not been set, // all elements initialize to 0 when first created System.out.println(arr[9]); /* Illegal operations! */ // an exception is thrown since arr can only hold 10 elements System.out.println(arr[11]); // there is no shorthand syntax to get the last element of an array System.out.println(arr[-1]); // length is a variable, not a method System.out.println(arr.length()); // arr can only hold ints arr[2] = "Hello"; List A List is an class that is part of the Java Standard Library which allows for dynamic insertion and deletion of elements. You can't just create a List though, you have to use one of its subclasses, which includes ArrayList, and you have to use generics as well. Think of an ArrayList as a dynamic version of an array, and is the closest thing to Python's list datatype. // Make sure to import these at the top of your class! import java.util.List; import java.util.ArrayList; // creates an ArrayList of ints List myList = new ArrayList<>(); // adds 5 to myList myList.add(5); // prints the 0th element of myList, which is 5 System.out.println(myList.get(0)); myList.add(6); // removes the last element of myList, which is 6 int removed = myList.remove(); // prints the size of the list, which is 1 System.out.println(myList.size()); /* Illegal operations! */ // myList only has 1 element System.out.println(list.get(5)); myList.remove(); // removes 5 // throws an exception since the list is empty myList.remove(); // throws an exception since myList can only take ints myList.add("Hello"); // you can only use bracket notation on arrays System.out.println(myList[0]); // size is a method, not a variable System.out.println(myList.size); Other Info There are lots of other List subclasses you can use depending on the kind of operations you want to do on it, such as Queues, LinkedLists, and PriorityQueues. The reason why we use Integer instead of int when working with Lists is a limitation of the Java language. Generic types can only be object types, but primitive types, such as int, double, boolean, etc., are not object types, and thus we have to use autoboxing to work with primitives in this manner.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ java: is arraylist better than arrays?
r/learnprogramming on Reddit: Java: Is Arraylist better than Arrays?
June 23, 2024 -

I've been programming in Java for 10 months now (as a subject in school) and i was always curious whats the biggest difference between Areays and Arraylist. I know that if i had an Arraylist named 'list' and i wrote System.out.print(list) it will print all the things that the list contains. Something that Arrays can't do that easily. But whats the actually biggest difference?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ lists vs arraylist vs arrays
r/learnjava on Reddit: Lists vs ArrayList vs Arrays
January 6, 2018 -

As a beginner just getting into java, I feel very comfortable with arrays; Iโ€™ve already built multiple programs using arrays.

That being said, Iโ€™ve recently discovered the List and ArrayList functions and Iโ€™m wondering how they differ from Arrays, how they may be similar to Arrays, and in what situations I would use Lists or ArrayLists as opposed to just an array.

Also, is it bad relying on Arrays to do a lot of programs? For example, Iโ€™ve used Arrays to find minimum values of pairs, when I couldโ€™ve just used GetMin function instead. Are there any downsides to using Arrays?

Thanks in advance.

Top answer
1 of 2
6

this is a nice example of why it's helpful to know data structures. it's not to solve leetcode problems, it's to know when to use what. so you know what an array is, you declare an array size and you can access elements with O(1) run time, but if you want to reorder things, you have to shift all the elements downstream, which takes more time.

"List" in Java is just in interface, you have to specify what type of list you want to use. There are several list types to choose from, most commonly a LinkedList or ArrayList. An interface is just a set of Methods, so whatever list type you create will have those methods to choose from, but they may be implemented differently behind the scenes.

An ArrayList in Java is maybe somewhat misleadingly named. It's just a dynamically sized array that has the Methods of a list. You don't have to declare a size for an ArrayList. It will automatically allocate a size for you, without telling you because you don't care, and if you reach the limit of the allocated size it'll add some more size for you without you needing to do anything. I think it creates a new, bigger array to do this, but I'm not 100% sure. So, use an ArrayList when you want the O(1) access run time of an array, but the size of the array is unknown or changing. If you're familiar with Python, what Python calls a List is essentially the same thing as what Java calls an ArrayList.

2 of 2
1

The general rule is that if you know or can calculate the needed size of a collection, you would use an array.

Not really. Even if you know the size up front you can use an arraylist just fine. It's backed by an array after all and you have all the conveniences of the collections (streams, copying, etc.) and all the different API/library methods that take lists but not arrays. Just mentioning this so beginners don't get the impression they should use arrays if they know the size up front.

Think of lists as the "backup option" when you can't use an array because you don't know what size you need the array to be.

So no; lists are not a 'backup option' at all. The collections are the primary collections you'll be using. Using 'just' arrays are something you very rarely see in code, even where you know the size up front or the list is hard-coded.

Top answer
1 of 5
34
To put it simply, ArrayList is a dynamically allocated Collection. You can add and remove and it will automatically resize for you. It also utilizes iterators and other fun and handy utilities to manage the array. ArrayList implementation is essentially just manual resizing of normal array[] An array[] is a data structure. It is a basic functionality of java which requires manual allocation and resizing (generally done by initializing a new array with a greater or lesser length and copying over the previous array with its added or removed item) I have no clue what a native array vs an array I've never heard of the term native array. I'm betting it just means a normal array built into java aka what I discussed in previous paragraph. Edit: I'll also add that there would probably be a very insignificant performance improvement using a built in data structure versus casting to put into an array list but that is very here or there. Some database libraries can also only store array[] but once again it is case dependent. Just some things to think about. Overall I tend to use arrays when dynamic allocation or casting isn't needed and ArrayList for pretty much every other scenario. Remember, the less casting the better!
2 of 5
5
You only have one array type in Java for instance: int[] nums = new int[10]; ArrayList is just one implementation for a List type (another one would be LinkedList). All classes that implement class A by contract must have all functionality class A has and more. If you look at the List docs you'll see that one of the implementations is Stack meaning that a Stack in Java has all the functionality of a List + the methods you see here .
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ the use of arrays v.s arraylists in java.
r/learnprogramming on Reddit: The use of Arrays v.s ArrayLists in Java.
May 31, 2012 -

I am an intermediate level programmer (non-professional) and there is something I have been wondering. What is the purpose of an array vs and array list? When would you use on over another? Surely an ArrayList is more convinient, as you do not need to say the specific index to put the data in by using x.add(y)? Any explaination would be greatly appreciated.

Top answer
1 of 3
9

Arrays and ArrayLists are both used for the same thing (storing more than one object), but are used in different places.

The main difference between the two is that ArrayLists can "grow" but arrays can't. That is to say, when I make a new array, I have to tell Java how big it is. Like so:

int[] bunchOfNumbers = new int[50];

This makes an array that can hold 50 integers.

However, if we were to do this with an arraylist, we don't have to tell it how big to go, so we can just write :

ArrayList<Integer> bunchOfNumbers = new ArrayList<Integer>();

So, what do these differences mean?

Well, for one thing, we can't add any more than 50 numbers to our array. On the other hand, we can add as many numbers to the ArrayList as we damn well please.

The downside to ArrayLists is hard to understand without knowing how ArrayLists work, so I'll do that first.

An ArrayList is, at its core, an array. However, because a standard Array can't grow, it needs to pay attention to if you are going to over-extend. If you are, it creates a new bigger array, copies over the elements in the old array, and then adds your new value to it.

Now we can understand that adding things to an ArrayList can take time because we have to copy the previous array when it has to grow.

So, to conclude
Array

  • Very fast

  • Can't grow past their initial limit.

ArrayList

  • Can be slower

  • Can grow very large on demand

If you have any questions, feel free to ask. There are also other Lists in java that might be of use to you like the LinkedList, which has very fast insertion and deletion but is slow if you need to pull a specific element out of it.

2 of 3
1

Use an array when you know exactly what size it's going to be and it won't change,

an ArrayList when you don't,

and a LinkedList if you plan on inserting and removing a lot.

๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ can someone please explain the difference between list and arraylist like i'm five?
r/javahelp on Reddit: Can someone please explain the difference between List and ArrayList like I'm five?
October 2, 2021 -

What exactly is the difference between

List<String> inputs = new ArrayList<>()

and

ArrayList<String> inputs = new ArrayList<>()

I've read some stuff about it but I'm a bit confused and I don't feel like I understood completely. Can someone explain the difference with some examples? Thank you!

Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ using arraylist vs list in java
r/learnprogramming on Reddit: Using ArrayList vs List in Java
January 25, 2022 - Share ... List is just an interface. List.of was added to make a shortcut to create an immutable instance of a list from some given items (which just so happens to be an array list internally)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ difference between arraylist implementing a list vs a arraylist
r/learnjava on Reddit: Difference between ArrayList implementing a List vs a ArrayList
May 26, 2021 -

What's the difference between? Since List is just an interface, than wouldn't arry1 and arry2 be the same thing? Any method changes between arry1 and arry2? Why should I use one over the other?

List<Integer> arry1 = new ArrayList<Integer>();  

vs

ArrayList<Integer> arry2 = new ArrayList<Integer>();  

Which one is more beneficial to use?

Top answer
1 of 5
39

The difference is in how they are accessed.

For array1, from that point on it is, for all intents and purposes, some kind of List. Under the hood it's secretly actually an ArrayList, but any methods that the ArrayList has which List doesn't are not available to you. This isn't the case for array2; you get to treat it like a full ArrayList.

This is an important concept in object oriented programming. List is an interface; it describes, in an abstract way, what you can expect some kind of List to do. ArrayList is an implementation of that interface. To see this in action, try assigning array1 to a LinkedList. You'll notice that this works with no issues despite not being an ArrayList; this is because both ArrayList and LinkedList are, despite their differences in approach, still Lists. But this doesn't work for array2, because it's more specific; while ArrayList and LinkedList are both Lists, a LinkedList is not an ArrayList.

The benefit of this is when you don't care what kind of list is being used, you just care that it's some kind of list.

TL;DR under the hood they're the same, but using the interface lets you hot swap it own for a different kind of List.

2 of 5
10

Well observed, List is indeed an interface, while ArrayList is an implementation on it. The benefit of using List is making it more flexible should you choose to switch to a different implementation of it in the future, or even use it to receive other types of List from other methods without having to change your original code.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ arraylist vs arrays?
r/learnjava on Reddit: ArrayList vs Arrays?
December 16, 2015 -

Hey! I am currently going through the http://mooc.cs.helsinki.fi/ tutorial to learn Java, and it is great so far! I seem to pick up things quite easily. But there is one thing I don't really understand, mainly because I am missing some context I think. But why do we make arrays holding String values like so?
ArrayList<String> varName = new ArrayList<String>();
varName.add("title1");
instead of just:
String[] varName = { "title1", "text2" };
since both seem to work just fine? Or do I use both syntaxes at different times?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ what is the diffrence between list and arraylist?
r/learnjava on Reddit: what is the diffrence between list and ArrayList?
December 17, 2022 -

we just learned about those two, and they seem to be pretty much the same.

was hoping to understand the diffrence

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ list vs arraylist
r/learnjava on Reddit: List vs ArrayList
March 25, 2017 -

In the MOOC online Java course (part 2), the web site shows the following code.

ArrayList<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
finnish.add(reg1);
finnish.add(reg2);

The exercise code provided uses the following

List<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
finnish.add(reg1);
finnish.add(reg2);

From reading posts and comments here, I understand why the second way is better but what I don't understand is what benefit it gives me in this case. For example, in another thread, the class hierarchy of Animal, Cat and Dog was used with the speak() method. I understand that having a list of Animals allows me to put any Animal derived type in there and process them while calling the specific sub-class method.

I can see the same benefit applying to a list of potentially different list types. Is there a benefit if the code only has one ArrayList?

๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ can someone please explain to me when i would use array vs arraylist? using some real world example?
Can someone please explain to me when I would use Array vs ArrayList? Using some real world example? : r/java
July 17, 2016 - Others here have effectively already said that with different wording. In the cases of using arrays of primitives or just in terms of reduced overhead relative to ArrayList arrays have performance benefits and lower GC costs. If you are building a framework you would look to use arrays on ...
๐ŸŒ
Reddit
reddit.com โ€บ r โ€บ learnprogramming โ€บ comments โ€บ 57z2bx โ€บ java_arrays_vs_lists
r/learnprogramming - Java Arrays vs Lists
October 17, 2016 -

Hi guys, I was wondering what the difference is between arrays and lists, what the pros and cons are to each, and when one would be preferable over the other. I'm in an intro java course and have a strong grasp on how arrays work, but our class never mentioned lists. However, they seem like a useful data structure and I'm curious as to how they work. Thanks in advance to anyone who responds!

Top answer
1 of 3
4

First of all, do you understand that List<E> is an interface, not an implementation? You can't actually instantiate List<E>, but you can use something like ArrayList<E> or LinkedList<E> or Vector<E> or whatever. And each of those implementations can have very different properties, e.g. indexing is O(1) for an ArrayList but O(n) for a LinkedList. So you can't really just talk about an abstract List like that.

But the main difference is that an array has a fixed size that cannot be changed after it's created. An the array is a built-in type, which has very little in the way of a rich API. Its only members are length, clone(), and the members inherited from Object. That's it. The various List<E> implementations are collection classes which have much more functionality, e.g. methods for insertion, appending, removal, iteration, sorting, searching, etc. And collections have dynamic sizes, so they expand and contract as you add and remove data, in contrast to an array that always has a fixed size. Collection classes play nicely with newer Java features, like Java 8 streams and such.

One of the downsides of using a collection class it that it can only work with reference types, not primitives. If you want an ArrayList of int, you have to use ArrayList<Integer>, and live with auto-boxing of int -> Integer. This is not great for performance, so in certain cases when you're working with primitive types and the fixed size is not an issue, then an array is probably a better choice. Otherwise you are almost always better off with an actual collection.

2 of 3
2

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ what is the difference between an array and lists?
r/learnjava on Reddit: What is the difference between an array and lists?
February 25, 2026 -

Hey everyone,

I've been studying through the course on mooc.fi and just now I faced this paragraph:

"Next let's explore algorithms associated with retrieving and sorting information. While the following examples utilize arrays, the algorithms shown will also work with other data-structures meant for storing information, such as lists."

Turns out I didn't really infer that array and lists are two different things as I've gone through the course. What exactly is the difference between these two data-structures?

Thank you in advance!

๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] when to use list, arraylist etc. ?
r/learnprogramming on Reddit: [Java] When to use List, ArrayList etc. ?
September 3, 2016 -

Hey,

so I'm a little bit confused here. There are a lot of lists and I don't really get what's the difference or when I should instantiate a List as an ArrayList or when to instantiate an ArrayList as a List etc. I hope you get what I mean. Here is a code example I wrote:

public class ListTest {
    public static void main(String args[]){
        List<String> list1 = new ArrayList<String>();
        ArrayList<String> list2 = new ArrayList<String>();

        list1.add("Hello");
        list2.add("Hello");

        System.out.println(list1.get(0));
        System.out.println(list2.get(0));
    }
}

What I'm asking here what is the difference between List and ArrayList and why can I instantiate a List as an ArrayList? To me it looks like they do exactly the same.

I hope someone can ELI5 that to me.

Top answer
1 of 4
45
Bad analogy: Using List is like saying "could you pick up peanut butter from the store?" Using ArrayList is like saying "could you pick up some Skippyยฎโ„ข in the 16oz size from the store?" List is an interface. It can be used to refer to any class that implements the given set of methods and behaviors. ArrayList is a specific implementation of that interface that is backed by an array. You don't necessarily need to use an array to implement the interface, you could use a linked list (i.e. LinkedList.) For example, if some method takes List as a parameter type then it's saying "I don't care what kind, as long as it's got the basic properties of a list." If a method takes ArrayList then it's hinting that there is no such flexibility, and it must be this particular implementation of a list. And you can't instantiate List. Your example is not doing that. In both cases you are instantiating ArrayList. That's the type that is used with new. The type of the reference does not have to be the same as the type of the referent. You are creating two references of different types, but they both refer to objects of type ArrayList.
2 of 4
3
If you've ever wondered what the phrase "programming through interfaces" means, this is why. Basically, List is an interface. ArrayList is one of several implementations of the List interface, with each implementation specializing in some special behavior. ArrayList is the default implementation with the most common behaviors you'd expect of a list. The reason List list1 = new ArrayList(); is better than ArrayList list2 = new ArrayList(); is because an interface is on the left side of the equation. Recall that the left side of the equation is the declaration and the right side is the value you are assigning to that declaration. Thus, the former allows you to switch out the implementation to something else, for example: List list1 = new LinkedList(); The reason this is important isn't easy to see here because this example is very simple. If you project this example into a large application with hundreds of classes, each with dozens of methods and each method requiring specific types of data to do its work; standardizing that data becomes important. We achieve that standardization through the use of interfaces.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ array vs array list
r/learnprogramming on Reddit: Array vs Array list
August 5, 2018 -

I was asked this question during my interview yesterday that arraylist uses array internally but array's are immutable and arraylist's are mutable. How is this possible. I know about mutable and immutable part and I explained to him that but I don't know about how it works internally there's no hope for this question on the internet. So some of you kind people can help me out. Thanks in advance.