Array Declaration In Java Using Netbeans IDE 7.1

Introduction

This article explains how to declare an array in Java using the NetBeans IDE 7.1.

What is array

In general, an array is a group of items having the same features or we can say that are of the same kind, like types of cars, bicycles, or any group having the same property.

In terms of computer programming languages, an array is a group of objects or a collection of variables that are all of the same type. An array is a collective name given to a group of similar quantities. All elements of any given array must be of the same type, in other words we can't have an array of 20 numbers, of which 10 are doubles and 10 are floats.

Arrays can be of multiple dimensions like:

  • one-dimensional array called a vector
  • two-dimensional array called a matrix
  • and multidimensional arrays.

How to declare an array in Java

There are the following ways to declare an array.

1. Assignment operator (=)

By using an assignment operator (=)  we assign an array. In this we initialize each element individually.

Example

Open the NetBeans IDE then open the file menu and select "New Project" then choose "Java application" then enter your project name (for example I choose "ArrayApp") and click on "Ok".

Create an ArrayAssignEx.java as follows:

package arrayapp;

public class ArrayAssignEx

  {

    public static void main(String[] args)

      {

        // declares an array of integers

        int[] x;

        // allocates memory for 4 integers

        x = new int[4];

        // initialize the array element

        x[0] = 10;

        x[1] = 11;

        x[2] = 12;

        x[3] = 13;

        System.out.println("Index 0: "+ x[0]);

        System.out.println("Index 1: "+ x[1]);

        System.out.println("Index 2: "+ x[2]);

        System.out.println("Iindex 3: "+ x[3]); 

      }

  }

 

Output

Run the application. You'll get the following output.

Fig-1.jpg

2. Using a for loop

By using a for loop, Java reduces coding lines, since we don't need to use different outputs for each index.

Create "ArrayUserAssignEx.java" as follows:

package arrayapp;

public class ArrayUserAssignEx

  {

    public static void main(String[] args)

      {

        // declares an array of integers

        int[] x= new int[4];

        x[0]=10;

        x[1]=11;

        x[2]=12;

        x[3]=13;

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

          {

            System.out.println("Elements: "+ x[i]);

          }

      }

  }

Output

Fig-2.jpg

3.Declare at the time of using a list

It is possible to fill an array at the time of declaration. By using this we don't need to initialize each element separately so it will help in reducing the code and it is beneficial for large array lists.

ArrayDeclarationEx.java

package arrayapp;

public class ArrayDeclarationEx

  {

    public static void main(String[] args)

      {

        // declares an array of integers it takes memory according to elements size

        int[] x= { 10, 11, 12, 13};

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

          {

            System.out.println("Elements: "+ x[i]);

          }

      }  

  }

Output

Fig-3.jpg

4. Declare an array of string

This type of declaration deals with a string array. Like prints the name of an animal, human being, or any other group.

ArrayStringEx.java

package arrayapp;

import java.util.*;

public class ArrayStringEx

  {

    public static void main(String[] args) 

      { 

        String[] str = {"elephant", "cow", "goat", "dog", "lion"};   

        List<String> strList = Arrays.asList(str);    

        for (String a : strList) 

          { 

            System.out.println(a); 

          } 

      }    

  }

Output

fig-4.jpg


Similar Articles