bscodinglab

Array in Java

Arrays :- Arrays are used to store multiple values of the same data type. They can be one-dimensional or multi-dimensional. Java arrays have a fixed length, and the elements can be accessed using their index. The elements of an array are stored in contiguous memory locations. This means that the elements are stored one after the other in memory, without any gaps. Java provides several methods to manipulate arrays, such as sorting, searching, and copying.

There are two types of arrays in Java :-

  1. One -Dimensional Arrays :- These arrays have only one dimension. This means that each element of the array is stored in a single memory location.

To create a single-dimensional array in Java, you first need to declare the array type and the number of elements in the array. Once the array has been initialized, you can access the elements of the array using their index.

Syntax :-    dataType[] arrayName;

                                    OR

                      dataType[] arrayName={“value-of-array”};

Example :-

// Declare an array of integers

int[] myArray = new int[5];

// Initialize the array with some values

    myArray[0] = 10;

    myArray[1] = 20;

    myArray[2] = 30;

    myArray[3] = 40;

    myArray[4] = 50;

    // Print the values of the array

        for (int i = 0; i < myArray.length; i++)

                {

            System.out.println(myArray[i]);

       }

Output :-   10

20

30

40

50

This array can store 5 integer elements. The elements of the array can be accessed using their indices. The index of the first element is 0, and the index of the last element is 4.

You can also use a for loop to iterate over the elements of the array and perform some operation on each element.

2. Multi-Dimensional Arrays :-These arrays have two or more dimensions.Two-dimensional arrays in Java are arrays of arrays. They can be used to represent data in two dimensions, such as a table or a grid.

To declare a two-dimensional array in Java, you use the following syntax.

Syntax :-  data_type[][] array_name;

                                    OR

                   data_type[][] array_name ={ {“value-of-array”} };

Example :-

int[][] my_array = {{1, 2, 3}, {4, 5, 6}};

            for( int i=0; i<3;  i++ )

                 {

                                   for( int j=0;  j<2;  j++)

                             {

                                    System.out.print(my_array [i] [j]); // Statements Write hare.

                             }

                        System.out.println();

                 }

Output :–      1          2          3

                      4          5          6

 This array can store 6 integer elements. The elements of the array can be accessed using their indices. The index of the first element is [0][0], and the index of the last element is[2][3] .

Processing Arrays :-

While processing array elements, we use loops or foreach loops because all the elements in an array are of the same type and the size of the array is known.

The foreach Loops :- Java introduced a new for loop in JDK 1.5 known as the foreach loop or enhanced for loop, which enables you to sequentially traverse the entire array without using index variables.

Example :-

public class  Array-Demo

{

   public static void main(String[] args)

{

      int[] myList = {1, 2, 3, 4};

      // Print all the array elements

      for (int element: myList)

{

         System.out.print(element+”\t”);

           }

    }

}

Output :–      1          2          3          4

Passing Arrays to Methods

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array –

Example :-

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

public static void printArray(int[] list)

            {

                        for (int i = 0; i < list.length; i++)

                         {

                                    System.out.print(list[i] + ” “);

                        }

            }

Output :-

Creating Arrays Refrence with new keyword :-

We can create an array by using the new operator with the following Syntax.

Syntax :-

arrayRefVar = new dataType[arraySize];

Example :-

int[] List = new int[5];

// Initialize the elements of the array.

List[0] = 1;

List[1] = 2;

List[2] = 3;

List[3] = 4;

List[4] = 5;

// Print the elements of the array to the console.

for (int i = 0; i < List.length; i++)

      {

              System.out.print(List[i]+” ”);

       }

Output :- 1       2        3          4          5

We can also use the new keyword to create an array of any other data type, such as strings, doubles, or objects.

Array class in Java :- The Arrays class in the java.util package provides a number of utility methods for working with arrays. Some of the most common methods in the Arrays class include:

  • sort() :- Sorts the elements of an array in ascending order.
  • binarySearch() :- Searches for an element in a sorted array.
  • copyOf() :- Creates a copy of an array.
  • fill() :- Fills an array with a specified value.
  • equals() :- Compares two arrays for equality.