Core Java Tutorial

Define Java

C vs C++ vs Java

Java Virtual Machine

Java First Program

Java Data Types

Java Tokens

Java Keywords

Java Operators

Operator Precedence

Java Type Casting

Java Statement

Java If statement

Java Switch statement

Java Loops

Java Jumping Statement

Java Arrays 1D

Java Arrays 2D

Java Variable Size Array

Java Vector

Java Math Methods

Java String Methods

Java String Buffer

User-Defined Method

Java Method Overloading

Java Class & Object

Java Constructor

Java Inheritance

Java Interface

Java Packages

Java Multi-threading

Exceptional Handling

File Handling

Java Applets

Java DataBase Connectivity

Page Stats

Visitor: 287

Java - Arrays

An array is a collection of homogeneous (similar type) elements.

Some important points about arrays:

  1. Arrays are always stored in continuous memory allocation.
  2. An array can store multiple values which can be referenced by a single name followed by an index inside a square bracket.
  3. Array index is a number starting from 0, will be increased by 1, till the maximum specified array size.
  4. Array index cannot be negative
  5. An array can be an integer, character or floating data type can be initialized only once at declaration time.

Arrays can be categorized into 2 categories:

  1. One Dimensional Array or 1D-Array
  2. Two Dimensional Array or 2D-Array

One Dimensional Array

A one dimensional array is one directional array, which contain only one single row and multiple columns. One-dimensional array can be declared as follows:

int []a;
a = new int [100];
int []a = new int[100];
1D Array

Example 1: WAP to input 10 numbers in 1D-Array and print them.

package arrays1d;
import java.util.Scanner;
public class Arrays1d {
    public static void main(String[] args) {
        int a[] = new int[10], i;
        Scanner scan = new Scanner(;
        System.out.println("Enter 10 numbers: ");
            a[i] = scan.nextInt();
        System.out.println("Input numbers are:");
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
Input numbers are: 1 2 3 4 5 6 7 8 9 10

1D-Array can be initialize as follows:

int a[] = { 1, 2, 3 };

Example 2: WAP to initialize array elements.

package arrays1d;
public class Arrays1d {
    public static void main(String[] args) {
        int i;
        int a[] = {10,20,30,40,50}; 
        System.out.println("Array list is:");
Array list is: 10 20 30 40 50

Array methods:

Array Length: To find array length

int a = array.length;

Array copy: To copy one array elements into another array

int a[] = { 1, 2, 3 };
int b[];
b=a; // copy element of a into b

Array sort:

Arrays.sort (array_variable_name); // java.util