answersLogoWhite

0

What is an array and explain?

User Avatar

Anonymous

14y ago
Updated: 8/18/2019

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.

Arrays are efficient, but most of the time you'll want to use one of the Collection types from java.util (including HashMap, ArrayList, TreeSet). Collection classes offer more flexible ways to access an object (for insertion, deletion, and so on) and unlike arrays, can expand or contract dynamically as you add or remove elements (they're really managed arrays, since they use arrays behind the scenes).

Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring an array of primitives:

int[] Values; // brackets before name (recommended)

int Values []; // brackets after name (legal but less readable)

// spaces between the name and [] legal, but bad

Declaring an array of object references:

Ferrari[] Ferraris; // Recommended

Ferrari Ferraris[]; // Legal but less readable

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, Values is a reference to an int array object, and not an int primitive.

We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner:

String[][][] occupantName; // recommended

String[] ManagerName []; // Real bad coding practice, but legal

The first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, but if you write code like this, anyone who is going to manage your code in future is sure going to curse you badly.

It is never legal to include the size of the array in your declaration. Yes, before you ask me, let me admit that you can do that in some other languages, which is exactly why you might see a question or two that include code similar to the following:

int[5] races;

The preceding code won't make it past the Java compiler. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters and not when you declare the array variable.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What is an array and explain its advantages and disadvantages?

what is array? explain with its advantage.


What is array Explain with syntax?

an array is a collection of the same data type.


Explain the Different types of array?

one dementional array and two dementional array


Explain the design and application of arrays and how an array simplifies program development?

I need an example of a real-world array


Biotovoltaic cell array?

Can you explain 'biotovoltaic' please, not a word I know


Explain how an array of 5 x 46 can help you find the product of 5 x 46?

The number of elements in a 5 x 46 array = 5*46.


Explain linear search with an example?

Sequential search of an object with in an array of objects is called as linear search.


Explain how an array of 5x46 can help you find the product of 5x46?

46+46+46+46+46


What are the different types of array explain with examples?

There are two main types of array:1) Single dimensional array: These are arrays for which only one index is required to access the element stored in an array. They are stored in a contiguous memory location.Eg:int arr[10];//declarationarr[7] = 7;//initialization2) Multidimensional array: These are arrays for which more than one index is required to access the element stored in a specific location in the array. These are stored in a contiguous memory location row-by-row.Eg:int arr[5][5];//two dimensional arrayint arr[5][5][5];//three dimensional array


Writing in math explain how an array of 5 x 46 can help you find the product of 5 x 46?

The number of elements (cells) in a 5 x 46 array = 5*46.


Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


What is meant by irregular dimensional array?

An irregular dimensional array is a special type of multi-dimensional array.First we must understand that a multi-dimensional array is just an array of arrays. Each element in the array is, itself, an array of elements.A regular multi-dimensional array will be an array of size n, with each element containing a separate array of size m. That is, each sub-array has the same size.An irregular multi-dimensional array will be a multi-dimensional array in which each sub-array does not contain the same number of elements.Regular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4, 5}array[2] = new array{6, 7, 8}array[3] = new array{9, 10, 11}This regular array is an array of size 4 in which each sub-array is of size 3.Irregular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4}array[2] = new array{5, 6, 7}array[3] = new array{8, 9, 10, 11}This irregular array is an array of size 4 in which the size of each sub-array is not the same.