Table of contents
1.
Introduction
2.
Properties of Single Dimensional Arrays
3.
Declaration of Arrays
4.
Cloning of Arrays
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Single Dimensional Array

Author NISHANT RANA
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The array is a Data Structure that is used to store data of similar types. All the data is stored in a contiguous manner. This means that if the first element is present at the Xth memory location then the next data will be present at the (X+Y)th memory location(here Y is the size of the data type). We can access any element/data using the indices. Also, arrays are cache-friendly. Arrays are also used to implement various Data Structure like Stacks, Queues, etc.

Read Also this  Iteration Statements in Java, and Duck Number in Java.

Properties of Single Dimensional Arrays

  1. We can only add data of similar types in Arrays.
  2. We can randomly access any data with the help of their indices.
  3. We can not change the size of the array after initializing it.
  4. We can use the dynamically sized arrays too like ArrayList in Java and Vectors in C++. The size of these arrays can dynamically change during the RunTime.
  5. In arrays, contiguous memory is allocated as follows:-
    Suppose we made an array of Integers of size 3. It will be represented as follows:-

Also see, Swap Function in Java

Declaration of Arrays

C++

data_type variable_name[size];

Example

int arr[5];

Java

data_type variable_name[] = new data_type[size];

Example

int arr[] = new int[5];

 

Let us see the declaration in the code for Java using a problem to find the sum of array elements.

import java.util.*;


class Main {
public static void main (String[] args) {
    int a[] = new int[5];


    a[0] = 0;
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    a[4] = 4;
    
    int sum = 0;
    
    for(int i=0;i<5;i++){
        sum = sum + a[i];
    }
    
    System.out.println(sum);
}
}

 

We can also create arrays of Objects. Refer to the below code:-

import java.util.*;


class Main {
public static void main (String[] args) {
    Student students[] = new Student[3];
    students[0] = new Student(0, "Ram");
    students[1] = new Student(0, "Laxman");
    students[2] = new Student(0, "Shiv");
    
    System.out.println(students[0].name);
}
}


class Student{
    int id;
    String name;
    Student(int id, String name){
        this.id = id;
        this.name = name;
    }
}

In the above code, we have a Student class with id and name as its attributes. Inside the main function, we have made an array Student of size 3. 

Also see, Hashcode Method in Java

Cloning of Arrays

We can make the exact copy of the arrays using the clone() method in Java.

import java.util.*;


class Main {
public static void main (String[] args) {
    int a[] = new int[5];
    a[0] = 0;
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    a[4] = 4;
    
    int b[] = a.clone();
    
    for(int i=0;i<5;i++){
        System.out.println(b[i]);
    }
}
}

In the above code, we made an array ‘a’ and then assigned the copy of array ‘a’ to array ‘b’ using the clone() method. clone() method creates a new array and copies the exact values of the previous array in the new array. Try it on online java compiler.

Must Read Array of Objects in Java

FAQs

  1. What is an array?
    The array is a Data Structure that is used to store data of similar types. All the data is stored in a contiguous manner. We can access any data using the indices. Also, arrays are cache-friendly.
     
  2. How arrays are Cache-Friendly?
    In particular, arrays are contiguous memory blocks, so large chunks of them will be loaded into the cache upon first access. This makes it comparatively quick to access future elements of the array.

Key Takeaways

In this blog, we have covered the following things:

  1. We first discussed what are Arrays.
  2. Then we discussed how to initialize arrays and their example code.

If you want to learn more about Programming Language and want to practice some questions which require you to take your basic knowledge on Programming Languages a notch higher, then you can visit our here.

Also, see Array in Java

Check out this problem - Search In Rotated Sorted Array

Until then, all the best for your future endeavors, and Keep Coding.


Live masterclass