Arrays in java

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation its length is fixed.

When you need to store same type of data that can be logically grouped together then you can go for java array. Arrays are easy to iterate, easy to store and retrieve using their integer index. In java, nobody can deny using an array I challenge. I know you all started with public static void main(?). The argument for the main method is an array.

In java arrays are objects. All methods of an Object can be invoked on an array. Arrays are stored in heap memory.

Though we view the array as cells of values, internally they are cells of variables. A java array is a group of variables referenced by a common name. Those variables are just references to a address and will not have a name. These are called the “components” of a java array. All the components must be of same type and that is called as “component type” of that java array.

Java Array Declaration

String studentName[];

Or

String []studentName;

Or

String[] studentName;

Java Array Initialization

studentName = new String[90];

90 inside the square bracket say that you are going to store ninety values and is the size of the array 90. When you refer the array values, the index starts from 0 “zero” to 89. An array index is always a whole number and it can be an int, short, byte, or char.

Java array initialization and instantiation together

int marks[] = {98959193,97};

Iterating a Java Array

public class IterateJavaArray {

 

 public static void main(String args[]) {

   

  String studentsName[] = {"Monty""John""Smith","Clark""Susen"};

  //java array iteration using enhanced for loop

   for (String studentName : studentsName){

   System.out.println(studentName);

    }

  }

}

Multidimensional Arrays

When a component type itself is an array type, then it is a multidimensional array. Though you can have multiple dimensions nested to ‘n’ level, the final dimension should be a basic type of primitive or an Object.

int[][] i = new int[5][10];

int[] []j = new int[5][10];

int[] marks, fruits, matrix[];

marks = new int[5];

fruits = new int[15];

matrix = new int[4][2];

in above examples i,j and matrix are multidimensional arrays.

Iterate a java multidimensional array

public classIterateMultiDimensionalJavaArray {

public static void main(String args[]) {

 

 int sudoku[][] = { { 213 }, {132 }, { 321 } };

 

  for (int row = 0; row < sudoku.length; row++) {

    for (int col = 0; col < sudoku[row].length; col++) {

      int value = sudoku[row][col];

      System.out.print(value);

            }

           System.out.println();

        }

    }

}

Sort a Java array

java api Arrays contains static methods for sorting. It is a best practice to use them always to sort an array.

import java.util.Arrays;

  

public class ArraySort {

  public static void main(String args[]) {

    int marks[] = { 949892,9397 };

    System.out.println("Before sorting: " + Arrays.toString(marks));

    Arrays.sort(marks);

    System.out.println("After sorting: " + Arrays.toString(marks));

  }

}

Copy a Java array

You can use the following options to copy a java array:

  • As illustrated above you can use the util calls Arrays. It contains copyOf method for different java types.
  • The most used class by a java beginner “System” has a static method to copy an array.
  • Using its clone method you can copy a java array. If the java array is multidimensional, it will be a shallow copy.
  • Write your own for loop iterating through the java array and copy elements yourself. (least preferred)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s