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

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).

Related

Is Java using pointer to access Array by index? [duplicate]

This question already has answers here:
How can I use pointers in Java?
(16 answers)
Closed 2 years ago.
I am curious. is java using Pointer like C or C++ to access array by index? when use C languange, x[a] can be converted to *(x + a). What about Java languange? its same with c and c++ or it just use Sequential search to access the element?
If it is an array of objects, it's essentially an array of pointers that reference those objects, this is not the case for primitive values however. Unlike c++, you cannot do pointer arithmetic in Java.
OK, i found an answer from this https://softwareengineering.stackexchange.com/a/105919
In Java, plain pointer arithmetics (referencing and dereferencing) don't exist anymore. However pointers exist. They call them references, but it doesn't change what it is. And array access still is exactly the same thing: Look at the address, add the index and use that memory location. However in Java, it will check whether or not that index is within the bounds of the array you originally allocated. If not, it will throw an exception.

Why does Java's List have "List.toArray()", but arrays don't have "Array.toList()"?

Arrays don't have a "toList" function, so we need "Arrays.asList" helper functions to do the conversion.
This is quite odd: List has its own function to convert to an array, but arrays need some helper functions to convert to a List. Why not let arrays have a "toList" function, and what's the reason behind this Java design?
Thanks a lot.
Because List instances are an actual object, while arrays are (for MOST intents and purposes) a primitive and don’t expose methods. Although technically arrays are an object which is how they can have a field length and a method call such as clone(), but their classes are created after compilation by the JVM.
Another point to consider is that toArray() is declared on an INTERFACE (specifically, the java.util.List interface).
Consequently, you must call toArray() on some object instance that implements List.
This might help explain at least one part of your question: why it's not static ...
As others have pointed out: An array in Java is a rather "low-level" construct. Although it is an Object, it is not really part the object-oriented world. This is true for arrays of references, like String[], but even more so for primitive arrays like int[] or float[]. These arrays are rather "raw, contiguous blocks of memory", that have a direct representation inside the Java Virtual Machine.
From the perspective of language design, arrays in Java might even be considered as a tribute to C and C++. There are languages that are purely object-oriented and where plain arrays or primitive types do not exist, but Java is not one of them.
More specifically focusing on your question:
An important point here is that Arrays#asList does not do any conversion. It only creates List that is a view on the array. In contrast to that, the Collection#toArray method indeed creates a new array. I wrote a few words about the implications of this difference in this answer about the lists that are created with Arrays#asList.
Additionally, the Arrays#asList method only works for arrays where the elements are a reference type (like String[]). It does not work for primitive arrays like long[] *. But fortunately, there are several methods to cope with that, and I mentioned a few of them in this answer about how to convert primitive arrays into List objects.
* In fact, Arrays#asList does work for primitive arrays like long[], but it does not create a List<Long>, but a List<long[]>. That's often a source of confusion...
From the modern perspective, there's no good reason why arrays lack the method you mention as well as a ton of other methods, which would make them so much easier to use. The only objective reason is that "it made the most sense to the designers of Java twenty two years ago". It was a different landscape in 1996, an era where C and C++ were the dominant general purpose programming languages.
You can compare this to e.g. Kotlin, a language that compiles to the exact same bytecode as Java. Its arrays are full-featured objects.
Arrays are primitives which don't have methods(With the exception of String) so you have to use methods from another class. A list is a reference type so it can have methods.

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

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?

Java arrayList vs. C++ vector [duplicate]

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/

Categories