Java Arrays

121 views
optmiize_your_resume_1

In this article you will learn about java arrays , one-dimensional array , character array and 2-D array.

Array is a data structure which stores elements having similar data type.

  • It is a non-primitive data type.
  • The memory allocated to values stored in array is contiguous.

In order to access each elements of an array we use index numbers. Index number begins with 0 and ends with size-Here size refers to the total elements in an array.

If the array is of size 11 , it means the number of elements in that array is 11 and the last element will be stored at index 10.

One-Dimensional Java Array :

In one-dimensional array we have a row only. In order to declare an array ,we need to mention the data_type and the name of the array.

Initially, zero is stored in an integer array if it is not initialized.

Java Arrays
//declaration data_type array_name[] = new data_type[size]; //assigning value data_type array_name = new int[size] {n1,n2,n3,n4,n5....}; or data_type array_name[] = {n1,n2,n3,n4,n5....};
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)

Example 1:

class arrayexample { public static void main(String args[]) { int arr[] = new int[]{1,2,3,4,5}; for(int i =0;i<5;i++) { System.out.println(arr[i]); } }
Code language: JavaScript (javascript)

Output

1 2 3 4 5

In order to calculate the length of an array we use the length property of arrays.

int length = array_name.length;

Example 2:

public class chararray { public static void main(String args[]) { char array[]={'a','b','c'}; for (int i =0;i<3;i++) System.out.print(array[i]+" "); } }
Code language: PHP (php)

Output

a b c

2-D Java Array:

In 2_D Array we have multiple rows and columns. Here, the first square bracket( [ ] ) refers to number of rows whereas the second square bracket( [ ] ) refers to the number of columns in an array.

//Declaring 2D Array int array_name[ ][ ] = new int[row][coloumn]; //Initializing at the time of declaration int array_name[ ][ ] = { {n00,n01,n02....}, {n10,n11,n12....}, {n20,n21,n22....}.....};
Code language: JavaScript (javascript)
public class ddarray { public static void main(String args[]) { int arr[ ][ ]={ {1,2,3,4}, {5,6,7,8}, {9,2,4,6} }; for(int i=0; i<3; i++) //outer loop for rows { for(int j=0;j<4;j++) //inner loop for coloumns System.out.print( arr[ i ][ j ]+" "); System.out.println(); //for next line } } }
Code language: PHP (php)

Output

1 2 3 4 5 6 7 8 9 2 4 6

Pros and Cons of Java Arrays:

Pros:

  • Implementation is easier as compared to linked list.
  • Accessing element is relatively easier and faster than list and stack.

Cons:

  • Its neither shrinkable nor expandable.

Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

20%off for Ink 280 281

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More