This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Array or List in Java. Which is faster?
I started a project where I will be constantly accessing an Arraylist that will never be changed. Would it be faster to access an ArrayList or an Array and by how much?
The difference will probably not be noticeable as the JVM will most likely optimise the call if you use it very often. And using List will give you a more robust and flexible code, so I would personally use Lists.
There is only one way to know: test it (after having read this post about how to test the performance of a Java application).
if its constant, how about an Enum, you can add logic directly to the enum instances.
Related
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 3 years ago.
How do I decide which data structure to use for a more efficient program? I mostly use ArrayList.
I checked out other questions but I was not fully satisfied. If someone could help me I would be really happy.
As ArrayList and LinkedList both implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations. ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array. LinkedList is implemented as a double linked list. Its performance on add and remove is better than ArrayList, but worse on get and set methods. So basically when you working with data that needs to be frequently added or removed from the list you would like to go for the LinkedList.
Very much a beginner question here, but hopefully a pertinent one.
I've been attempting to teach myself Java by way of coding a crappy little roguelike.
Since I discovered the collections framework, I've found that I'm using arraylists absolutely everywhere - so much so in fact that I find myself worrying I’m being woefully inefficient by using them in places where a regular array would suffice.
Thus my question is this: Under what circumstances should I favour using an arrayList over a regular array (or vice-versa) and why? Is there some kind of simple rule of thumb to help me pick which I should be using for any given task?
I refute that this duplicates Array or List in Java. Which is faster? - my question asks in which situation one is more methodologically sound than the other, and not which is generally quicker for any given task.
As said in Effective Java, one should prefer Lists to arrays.
One of the major differences is that arrays are covariant by their type and thus need accurate handling. Also, their type is reified and they do not mix well with generics.
But the implication is that arrays are able to work with primitives while generic collections aren't: they have Objects inside. So you might prefer arrays in performance critical parts of your code to avoid primitives boxing-unboxing.
If you know that your collection will always be a fixed length then use array.
If your collection is variable in length, I.e it could hold 1,5,100 values then use arraylist.
Example.
An application that asks the user a series of questions, the user can try get the answer right as many times as they like.
You create an array of possible answers to a question, you know there will only ever be 5 possible answers for each question, you would use an array of length 5 to store the possible answers.
You decide to create an array of all the answers the user submits, they could submit any number of answers, you'd store these in an arraylist as the user could give 1 or 100 answers before getting the question correct, a fixed length array here wouldn't do the job.
Hope that helps
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
When to use a List over an Array in Java?
(9 answers)
Closed 9 years ago.
I know Lists make things much easier in Java instead of working with hard-set arrays (lists allow you to add/remove elements at will and they automagically resize, etc).
I've read some stuff suggesting that array's should be avoided in java whenever possible since they are not flexible at all (and sometimes can impose weird limitations, such as if you don't know the size an array needs to be, etc).
Is this "good practice" to stop using arrays at all and use only List logic instead? I'm sure the List type consumes more memory than an array and thus have higher overhead, but is this significant? Most Lists would be GC'ed during runtime if they are left laying around anyways so maybe it isn't as big of a deal as I'm thinking?
I don't like dogma. Know the rules; know when to break the rules.
"Never" is too strong, especially when it comes to software.
Arrays and Lists are both potential targets for the GC, so that's a wash.
Yes, you have to know the size of an array before you start. For the cases when you do, there's nothing wrong with it.
It's easy to go back and forth as needed using java.util.Collections and java.util.Arrays classes.
I think a good rule of thumb would be to use Lists unless you NEED an Array (for memory/performance reasons). Otherwise Lists are typically easier to maintain and thus less likely to cause future bugs.
Lists provide more flexibility/functionality in terms of auto-expansion, so unless you are either pressed for memory (and can't afford the overhead that Lists create) or do not mind maintaining the Array size as it expands/shrinks, I would recommend Lists.
Try not to micromanage the code too much, and instead focus on more discernible and readable components.
It depends on the list. A LinkedList probably takes up space only as it's needed, while an ArrayList typically greatly increases its space whenever its capacity is reached. Internally, an ArrayList is implemented using an array, but it's an array that's always larger than what you want. However, since it stores references, not objects, the memory overhead is negligible in most cases and the convenience is worth it, I believe.
I would have to say I follow this approach of using the Collections framework where I might otherwise have used an array. The collections offer you many benefits and convenience over arrays but yes there is likely to be some performance hit.
It is better to write code that is easy to understand and hard to break, arrays require you to put in a lot of checking code to ensure you don't access bits of the array you shouldn't or put to many things in it etc. Given that the majority of the time performance is not a problem it shouldn't be a concern.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why aren’t arrays expandable?
I am starting to learn Java as my Computer Science school's assignments require this language and I'm liking the language. However, I've seen that Java arrays are not expansible - that is - you must declare their length before using them and it can't be changed further.
I'd like to know exactly why is that? Why the Java language designers chose to to make arrays as such? I guess it's for performance concerns but I'm not sure.
Thanks everyone in advance.
I'd like to know exactly why is that? Why the Java language designers chose to to make arrays as such? I guess it's for performance concerns but I'm not sure.
They designed primitives and arrays to be as simple and low level as possible. They don't do anything special and arrays don't use Object Orientated design at all. i.e. they only have a few useful methods, none specific to arrays.
The idea was that you would write higher level collections such as Lists using these low level constructs.
Java arrays are almost as simple as C arrays. C array is just a allocated memory region of n*m bytes where n is the number of elements in the array and m is the number of bytes needed to store a single element.
Then only thing Java added here is length and probably toString(). All other features can make array performance ineffective. Collections do that very well. Moreover collections are written in java itself that makes them portable.
Why the Java language designers chose to to make arrays as such?
Arrays are one of the programming data structures provided by the language. if you make Array also expandible, it'll become similar to ArrayList.
So, i guess because of two reasons:
To make Java similar to previous languages on basic constructs.
To remove duplication.
Arrays occupy consecutive memory locations and the compiler cannot make sure that the locations following the end of the array are available to be added to the array.
That is why many people Use LinkedList or ArrayList
This question is answred
Why aren't arrays expandable?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the C++ version of Java's ArrayList
I was reading a book called "Cracking the Coding Interview" and most (all?) of the code is in Java and arrayList is used a lot. During an interview, would using a vector instead of arrayList be acceptable if the language is C++? I'm asking because I haven't seen even one example of C++ code for similar questions using a vector but I'm not sure if there's a significant difference or not.
And is there also an equivalent in C?
The answer is two-fold: Firstly, you cannot compare utility classes between C++ and Java like that - different languages come with different cultures, naming conventions etc. If there was a Vector class in a C++ library, there's no connection whatsoever to any Vector class in Java, except for the name.
Secondly, the Vector class in Java is in practice deprecated, and I would discourage you from using it. In fact, forget about it :) The combination of List and ArrayList is the way to go. Use interfaces where you can, say:
List myList = new ArrayList();
Example deliberately missing generic typing.
Ignoring synchronization, the main difference between Vector and ArrayList is that Vector is a resizable array (similar to a C++ STL Vector) and ArrayList is a List that happens to be backed by an array.
arraylist-vs-vectors
In the back end, they are both arrays with functions on top to assist the programmer. Now, how different are they fundamentally?
check here :
http://www.reddit.com/r/learnprogramming/comments/l6o65/arraylist_java_vs_vectors_c/