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.
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?
Videos
It seems like ArrayLists are a lot more versatile and easier to work with, so in what cases would you choose to use an array instead?
Say I need to do something like get a list of numbers from the user, so the user enters comma separated values and I get this input as a string, then pass it to a function that parses this input. The way I would do it is to return it as an ArrayList, not an array. Is this the "right" way to do it?
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.
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.
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.
Java seems to have so many different arrays. Iโm guessing Arraylists are basically the lists in python?
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.
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.
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.
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!
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?
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.
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.
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?
A simple array is not dynamic, meaning that you only get two spots in the array where as the array list will expand as needed. In your example the array would error if you added a third string while in the list you can add strings effectively infinitely (not LITERALLY true but true enough for practical purposes). Now why would you want to use the simple array because it's less overhead and faster. With the array list you want to pass the size you think it will be to the constructor if you can, because basically the way the array list works is when it hits its size limit it makes a new array list that's double the length and copies the old contents into it, so the fewer times you have to do that the better.
ArrayLists are different from arrays in the two ways:
-
ArrayLists can change in memory length, meaning you can add stuff to them later without the compiler giving you an error.
-
ArrayLists can also hold class objects. I don't know if you've gotten to classes in your tutorial yet, but this is a big deal.
we just learned about those two, and they seem to be pretty much the same.
was hoping to understand the diffrence
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?
One plus of this technique is that later you can change your mind about what implementation to use for List and you only need to change one thing (i.e. the moment of construction).
Basically the idea is to use the super classes/interfaces as much as possible since you don't know when you might need to use it differently. I.e. you could even go for the interface Collection, but then you wouldn't be able to add to it, which is why you take List, instead of ArrayList
The only real benefit I see to array is that it is slightly faster to get and set elements. However, arraylist seems much more simple if you want to add, remove, elements and the set/get methods are only slightly longer to implement. So from this it seems as if arraylist is much more superior. Why would you use an array over arraylist then?
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!
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.
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
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!
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.
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.