Java

How to declare and initialize java array in different ways using NetBeans

Description:

Java has an array for handling multiple values ​​of the same data type together. To use an array, it must be declared (defined) and initialized. Arrays make it easier to write code because you don’t have to declare multiple variables of the same data type.

In this article, we will explain how to declare and initialize arrays in Java.

  • How to specify the size with new at the same time as declaring the array
  • How to initialize at the same time as the declaration.
  • How to set the same value for all elements with Arrays.fill

This time, in order to remember these methods, I will explain various ways to use array declarations in an easy-to-understand manner!



What is Java array declaration (definition)?

How to declare an array

To use an array, you have to declare it first. Here’s how to declare an array:

int[] data1;

int data2[];

Array declarations can be written in either style, but the former style, in which the parentheses immediately follow the data type, is more common in Java.

However, it cannot be used as it is because the size of the array is not specified. I will explain how to use arrays in detail in this article, so be sure to check it out until the end!

How to set the size of an array

You cannot use an array just by declaring it, so you need to specify its size. To specify the size of the array, write as follows.

int[] data1;

data1 = new int[3];

An int type array with the number of elements “3” has now been created. Now that the size of the array is determined, we can use it by assigning values ​​as follows.

data1[0] = 1;

data1[1] = 23;

data1[2] = 456;


How to use “new” when declaring the array:

By specifying the size with new at the same time as declaring the array, the code can be written concisely. To specify the size at the same time as declaring the array, write as follows.

int[] data1 = new int[3];

Now you have declared the array and specified its size.

How to initialize an array in Java

Array initialization:

To use an array, you need to initialize it by assigning values ​​after declaring it. To assign a value to an array, specify the element number (index) in parentheses.

If you do not assign a value, it contains the default value “0 “. Let’s check the usage of array initialization with the following program.

package com.mycompany.javabasics;

/**
 *
 * @author Fawadkhan
 */
public class JavaArrayExample {

    public static void main(String[] args) {

        int[] data1;
        data1 = new int[6];

        data1[0] = 1;
        data1[1] = 2;
        data1[2] = 3;
        data1[3] = 4;
        data1[4] = 5;

        System.out.println(data1[0]);
        System.out.println(data1[1]);
        System.out.println(data1[2]);
        System.out.println(data1[3]);

        System.out.println(data1[4]);

        System.out.println(data1[5]);

    }

} 

output:

How to declare and initialize java array

This program assigns values ​​to an array one by one and displays the result. Here, the default value of “0” is displayed for data[5] because it has not been assigned a value.


value initialize at the same time as Declaration:

Here, we will explain how to initialize by assigning a value at the same time as declaring it. To initialize at the time of declaration, write:

int[] var = { 1, 2, 3,4,5,6,7……. };

The size of the array is automatically determined by the number of elements specified in initialization. Let’s check it with the following program.

package com.mycompany.javabasics;

/**
 *
 * @author Fawadkhan
 */
public class JavaArrayExample {

    public static void main(String[] args) {

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

        System.out.println(data1[0]);
        System.out.println(data1[1]);
        System.out.println(data1[2]);
        System.out.println(data1[3]);

        System.out.println(data1[4]);

        System.out.println(data1[5]);

    }

} 

output:
How to declare and initialize java array

In this program, the values ​​initialized at the time of declaration are displayed. From the execution result of the program, we can confirm that it is initialized at the same time as the declaration!


How to set the same value for all elements with Arrays.fill

If you want to set the same value to all elements of an array and initialize it, use the fill method of the Arrays class. Let’s check it with the following program.

package com.mycompany.javabasics;
import java.util.Arrays;

/**
 *
 * @author Fawadkhan
 */
public class JavaArrayExample {

    public static void main(String[] args) {

       
 
        int[] data1 = new int[6];

            Arrays.fill(data1, 3);

            System.out.println(data1[0]);
            System.out.println(data1[1]);
            System.out.println(data1[2]);
            System.out.println(data1[3]);
            System.out.println(data1[4]);
            System.out.println(data1[5]);

        }

    } 

output:

How to declare and initialize java array

In this program, Arrays.fill initializes all elements of the array to “3” and displays their values. From the program execution results, we can confirm that Arrays.fill has changed the values ​​of all the elements in the array!

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button