What is an array in Java Please include the advantages and d

What is an array in Java? Please include the advantages and disadvantages of using an array, and how to declare one, pass one to a method, and how to use one with more than one dimension.

Solution

Arrays in java are a collection of similar tupes of elements and are of fixed size.

We can declare an array in the following way:

dataType[] arrayName; //this is the preferred way

or

datatype arrayNmae[];

We can also create arrays with the new operator.Eg:

arrayName = new dataType[arraySize];

We can also create an array by the following way:

If we want to access the array elements, we access them using the index. The array elements start from index 0 and the last element is at arrayName.length-1 position.

Advantages of java Array:

1. Because the arrays are collection of similar elements, sorting is very easy in arrays.

2. Because of the sequential placing of the elements, retrieving any data is alo easy.

3. Due to the indexing, we can easily locate the data in the array.

Disadvantages:

The only disadvantage of java arrays is that they are size bounded.

Passing an array into any method: Suppose we have an array named newArray which has all the String elements.

Multi-dimensional arrays: We can declare multi-dimensional arrays in the following ways:

dataType[][] arrayName; (or)

dataType arrayName[][];

For instantiating a multi-dimensional array we write:

int[][] array=new int[2][2]; //2 rows and 2 columns

We initialize the above array as:

What is an array in Java? Please include the advantages and disadvantages of using an array, and how to declare one, pass one to a method, and how to use one wi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site