Single and Multidimensional Arrays in Java

 Array

An array is simply a set of values with the same data type for each item. Even though it is made up of many pieces, an array is a single object. Arrays can be used to chunk an entire string of characters into single characters, such as a user name.

Java Array

·        An array in Java is an object that contains elements of the same data type.

·        Furthermore, the elements of an array are kept in a single memory location.

·        It's a data structure where we keep items that are similar.

·        In a Java array, we can only store a fixed number of elements.

·        The first element of an array is stored at the 0th index, the second element is stored at the 1st index, and so on.

·        The java.util class is a collection of utility classes for Java.

·        There are some methods available for arrays. These methods can be applied to an array to obtain the array's index and length.

·        Unlike C/C++, the length member can be used to determine the length of an array.

·        The sizeof operator is required in C/C++.

·        In Java, we can create single-dimensional or multidimensional arrays in the same way we can in C/C++.

·        Furthermore, anonymous arrays are a feature of Java that is not available in C/C++.

Advantages

·        In a single variable, an array can store multiple values.

·        When compared to primitive data types, arrays are faster.

·        Objects can be stored in an array.

·        Members of the array are stored in memory in a sequential order.

·        Using the indexes provided by arrays, we can access any element at random.

Disadvantages

·        Arrays are strongly typed data structures.

·        There are no add or remove methods in arrays.

·        The array's size must be mentioned.

·        The length is fixed. As a result, there is a risk of memory loss.

·        We must traverse the entire array to delete an element in an array, which reduces performance.

 

 

Types of Java Arrays:



Photo from www.educba.com

Single Dimensional Arrays

The 1D array is a single-dimensional array. It may only have one row or one column.

Syntax to Declare an Array in Java

  1. dataType[] array; (or)  
  2. dataType []array; (or)  
  3. dataType array[]; 

While declaring the array it is not important to mention the size of the array.

 

array = new int[5]; //creating an array

While creating we have to provide the size of the array.

We can declare and creating an array at the same time as follows:

int[] array = new int[5];

Initialization

Array[0] = 1 ; // value 1 is being stored at the 0th position in array

Array[1] = 2 ; // value 2 is being stored at the 1st position in array

Array[2] = 3; // value 3 is being stored at the 2nd position in array

Array[3] = 4; // value 4 is being stored at the 3rd  position in array

 

Array[4] = 5; // value 5 is being stored at the 4th position in array

We can declare, create and initialize an array in a single line as well

Int[] array = {1,2,3,4,5};

Or like below,

Int[] array = new int[] {1,2,3,4,5};

If we want to add some values to a particular position in the array, we may do it by providing the appropriate index number.

Eg: name_of_array[index_number] = value

In order to get a hold of a specific value in order to do some programming. How to find the value of a specific array element? For that, the concept of index no. exists in Array.

Syntax:

array[index]

Example





Output:





Accessing the elements of an array:

You can access an array element by using the array's name followed by an index enclosed in a pair of square brackets after it has been created.

An array's index or subscript indicates the position of an element within the array.

The first element in the array always has an index of 0 (zero), the second element has an index of 1, and so on. The last element's index is always one less than the array's total number of elements. arrayRefVar[index] is the syntax for accessing an array element.

Multi-dimensional Arrays

Multidimensional arrays are collections of arrays, each of which contains the references to other arrays. Tables are used to store data in multidimensional arrays (in row major order).

Initialization

One set of square brackets ([]) is appended to each dimension to produce a multidimensional array.

data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];

where data_type: Type of data to be stored in the array. For example: int, char, etc.

            dimension: The dimension of the array created.

            For example: 1D, 2D, etc.

            array_name: Name of the array

            size1, size2, …, sizeN: Sizes of the dimensions respectively.

Size of multi-dimensional arrays:

Multiplying the sizes of all the dimensions yields the total number of elements that can be stored in a multidimensional array.

For example:

The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

Similarly, array int[][][] x = new int[5][10][20] can store a total of (5*10*20) = 1000 elements.

Example:

2-D Array:




Output:




3-D Array:




Output


References:

https://medium.com/edureka/java-array-tutorial-50299ef85e5

https://www.javatpoint.com/array-in-java

https://www.programiz.com/java-programming/multidimensional-array

https://www.educba.com/arrays-in-java-programming/

https://javatutoring.com/java-one-dimensional-array/

https://ecomputernotes.com/java/jarray/accessing-array-elements-in-java

 

Comments

Post a Comment

Popular posts from this blog

Single and Multidimensional Arrays in Java