Arrays in Java are similar to C/C++ arrays, however there are some special unique things about them which make Java Arrays a little easier to work with in my opinion.
Like in C/C++ you can have single dimensional or multidimensional arrays.
Advantages to Arrays
Like a C/C++ array, the array in Java is a contiguous chunk of memory, which makes working with it fairly efficient from a speed perspective. Also, given the modern system, unless your array has tens of thousands of elements, you should be able to create an array of any data element you’d like, even objects, which we haven’t looked at closely yet.
The reason why you can have a large array of elements, even objects, is because objects are stored by reference (similar to pointers) by default, so if you can create the object, a small amount of space for the reference is relatively easy.
Of course, the old adage about it being easier to have an array, then 10, 15, or more uniquely named variables also works here. Plus, it’s easy to randomly access elements in the array, as long as we know the index for it.
A difference in Java Arrays compared to C./C++ is that the array is actually an object. Which means you get properties and methods to make working with the array even easier than with C/C++. One common attribute is the length
, which tells you how many elements are in your array. Now these elements may be empty, it only checks to see how big the array is.
A further advantage of the Java Array, because it’s an object, and it implements Clonable and Serializable. We’ve not talked about interfaces and implementing interfaces yet, but this will make it easier to duplicate an array and step through each element.
Disadvantages to Arrays
The biggest disadvantage of any array, in any language, is that it is a fixed size. You cannot change you mind and grow or shrink an array once it has been created.
Further more, this size must be found in a contiguous chunk of memory. You cannot break this into two or three pieces because you cannot make it fit, the array creation will simply fail. The good news to this is Java does dynamic memory allocation and memory management within the JVM. So it can attempt to rearrange elements in memory to make it room to create your array.
Creating an Array
Creating an array in Java is fairly simple. You simply declare it by defining your data type and the array name, with square brackets.
// general example for declaring an array
dataType []arr; // least common method
dataType[] arr;
dataType arr[]; // probably the most common method
Of course, you can also instantiate your array, which will give it its size.
dataType arr[] = new datatype[size]; // size can be a variable or literal
So an example would be:
int myArray[] = new int[10]; // create an array of 10 integers
The issue with the previous example, would be that the array still needs to initialized, as the values don’t exist yet. Luckily, you can declare, instantiate, and initialize the java array together by:
int myArray[] = {1,3,5,7,9,11,13,15,17,19,21};
The initial values are in braces, and the elements are separated by commas. The size of the array will be calculated by determining how many elements are contained within the braces.
Java Arrays was originally found on Access 2 Learn
One Comment
Comments are closed.