Introduction to Arrays

What is an Array ?

Java array is an object which contains elements of a similar data type. The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array can contain primitives (int, char, etc.) as well as object (or non-primitive) references of a class depending on the definition of the array. In case of primitive data types, the actual values are stored in contiguous memory locations.

introduction to arrays in V

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array
Introduction to Array in Java

Single Dimension Array :

A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one-dimensional array. The array subscript or index begins from 0.

Syntax: data_type array_name[Number_of Elements] ;

2-Dimension Array :

This array can be defined as an array of several one-dimensional arrays. It can be visualized in the form of a table with rows and columns. It is an array with two indexes. One index represents the row number and the other the column number.

Syntax: data_type array_name [row size][column size] ;

Multi Dimension Array :

A 2-D array can be defined as an array of several 1-D arrays. Likewise, a 3-D array can be defined as an array of several 2-D arrays. In general, an n-dimensional array can be defined as an array of several (n-1) dimensional arrays.

Example: arr[2][2][2] is a 3-D array with 2 two-D arrays. Each 2-D array has 2 rows and 2 columns.

Java code to demonstrate working of array

Problem Statement: Java Program to Print the Odd & Even Numbers in an Array

This is a Java Program to Print the Odd & Even Numbers in an Array.

Enter size of array and then enter all the elements of that array. Now using for loop and if condition we use to distinguish whether given integer in the array is odd or even.

Java Code

Run
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int n;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array: ");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements: ");
        for (int i = 0; i < n; i++) 
        {
            a[i] = s.nextInt();
        }
        System.out.print("Odd numbers: ");
        for(int i = 0 ; i < n ; i++)
        {
            if(a[i] % 2 != 0)
            {
                System.out.print(a[i]+" ");
            }
        }
        System.out.println("");
        System.out.print("Even numbers: ");
        for(int i = 0 ; i < n ; i++)
        {
            if(a[i] % 2 == 0)
            {
                System.out.print(a[i]+" ");
            }
        }
    }
}

Output:

Enter no. of elements you want in array: 5
Enter all the elements:
1
2
3
4
5
Odd numbers: 1 3 5 
Even numbers: 2 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Arrays

  • Introduction to Arrays – CC++ | Java
  • Introduction to 2-D Arrays – CC++ Java
  • Sorting of Array – C | C++ | Java
  • Array Rotation – CC++ | Java
  • Reverse an array or string- CC++ | Java
  • Find pairs in array with given sum – CC++ | Java
  • Sort the array in Waveform – CC++ | Java
  • Majority Element in Array – CC++ | Java
  • Boyer-Moore’s Voting Algorithm – CC++ | Java
  • K-pairs with smallest sum in 2 arrays – C | C++ | Java
  • Largest Sum Contigous SubArray – CC++ | Java
  • Maximum Average Sub-array of K length – CC++ | Java
  • Size of sub-array with max sum – CC++ | Java
  • Sub-array with given sum – CC++ | Java
  • Triplet that sum to a given value – CC++ | Java
  • Segregate 0’s and 1’s in array – CC++ | Java
  • Segregate 0’s 1’s and 2’s in array – CC++ | Java
  • Sort elements in array by frequency – CC++ | Java
  • Finding pythagorean triplets in an array – CC++ | Java
  • Reorder array using given indexes – CC++ | Java
  • Merging two sorted arrays – CC++ | Java
  • Minimum number of Merge Operations to make an Array Palindrome – CC++ | Java
  • Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – CC++ | Java

3 comments on “Introduction to Arrays”