Java arrayList vs. C++ vector [duplicate] - java

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/

Related

When to ArrayList

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

Array vs ArrayList: Dynamic Use

Both ArrayLists and Vectors make use of typical arrays internally. However, that leaves me thinking... why would I use ArrayLists when I can technically do the same thing using Arrays? Is convenience the only reason? Do performance-critical applications ever make use of an ArrayList?
Any tips would be appreciated.
I believe there are multiple reasons to prefer Lists over "implementing lists over arrays" or over "using arrays", but here are the two that I think are most important:
Lists have better support to generics than Arrays (you can, and should, read about it in "Effective Java" by Bloch - see Item 25)
If you ask about using ArrayList vs. implementing it yourself - I find it hard to believe that you'll do a better job than the guys that developed it in openjdk (Josh Bloch and Neal Gafter).
Yes, performance critical applications use ArrayList all the time. It's very unlikely that array access is the dominant factor in the vast majority of programs written in Java.
The ArrayList Collection interface is much richer than the functionality provided by built-in primitive arrays. This extra functionality will save you development time as well as debugging time by not having to write those algorithms yourself.
Additionally, many programmers are already familiar with the ArrayList Collection interface and thus by utilizing the existing standard libraries it will make your code easier to read and maintain for the long term.
One reason is that ArrayLists sizes are dynamic, arrays aren't.
The internal implementation of ArrayList is array only. but ArrayList is an wrapper class which is having more capabilities added to it. These capabilities are not available when you deal with Array directly.
For example,
Delete an element from array, you will have to implement logic if your are using an Array. But if you are using ArrayList, it will do the deletion for you.
Adding an element to array:
If you are using an array, you will have to implement the logic. But using an ArrayList, it is pretty easy.
You will find lot of methods in this ArrayList class that are handy for day to day use.
Hope this will help you.

Why Java supports arrays of arrays, rather than multidimensional arrays? [duplicate]

This question already has answers here:
Why doesn't Java have true multidimensional arrays?
(6 answers)
Closed 8 years ago.
What are the differences between multidimensional arrays and array-of-arrays?
Why Java supports arrays of arrays, rather than multidimensional arrays ?
Although your why question is probably unanswerable at this point (it would take one of the original creators of Java to answer it), you may note that a strong design principle of the original Java was simplicity. In that spirit all that Java supports is an array, which on its own gives you an array of arrays as just a special case: such an array whose component type is array.
About the only advantage of a true multidimensional array is the way its members are packed together, offering better cache locality. Such concerns were not high on the list of design priorities of original Java, although today they are getting a much greater share of the spotlight.
Refer to this topic for an in-depth review of pros and cons of multidimensional arrays.
Short answer: because the language was designed this way. But an array of arrays functions as a multidimensional arrays, so this is not really a limit on the language.
Probably the reason for this is that Java borrowed its array syntax from C and C++, and C and C++ multidimensional arrays are also accessed as though they are arrays of arrays. The difference is that in Java an array of arrays is an array of references to arrays (and thus the arrays in the array can have different lengths).

Why exactly are Java arrays not expansible? [duplicate]

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?

Speeds of Accessing Arrays vs. ArrayList Java [duplicate]

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.

Categories