Java Program to Print the Array Element Address When Base Address and Element Size is Given

In the previous article, we have seen Java Program to Convert Linked List to Array

In this article we will see how we can find the array element address while the base address and array element size is given using java programming language.

Java Program to Print the Array Element Address When Base Address and Element Size is Given

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see the solution to the problem statement.

Approach:

  • Ask the user to enter base address of the array.
  • Ask the user to enter the size of the array element in bytes.
  • Ask the user to enter the index of the element of which you need address.
  • Find address by adding base address with product of element size and element index.
  • Print the array element address.

Program:

import java.io.*;
 
public class Main 
{
    public static void main(String args[]) throws Exception
    {
        //Object of BufferedReader Class is created
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try
        {
            //Enter address greater than 0
            //Entering base address of array
            System.out.println("Enter the base address of the array : ");
            long baseAddress=Long.parseLong(br.readLine());
    
            //Entering size of array element int bytes
            //Enter size greater than 0
            System.out.println("Enter the size of the array element in bytes: ");
            long elementSize=Long.parseLong(br.readLine());
    
            System.out.println("Enter the index of the element of which you need address: ");
            long elementIndex=Long.parseLong(br.readLine());
        
    
            //checking if baseAddress or elementSize or elementIndex value is less that 0
            //then print that input is invalid
            if( baseAddress < 0 || elementSize <=0 || elementIndex < 0 )
            {
                System.out.println("Entered input is Invalid");
            }
            //else find the element address
            else
            {
                long elementAddress;
                //getting element address by adding base address 
                //with product of element size and element index
                elementAddress = baseAddress + (elementSize * elementIndex);
                System.out.println("Address of array element at index "+ elementIndex
                                                     +" is "+elementAddress);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
Output:

Case-1

Enter the base address of the array : 
2000
Enter the size of the array element in bytes: 
2
Enter the index of the element of which you need address: 
3
Address of array element at index 3 is 2006

Case-2

Enter the base address of the array : 
-2000
Enter the size of the array element in bytes: 
2
Enter the index of the element of which you need address: 
3
Entered input is Invalid


Case-3

Enter the base address of the array : 
2000
Enter the size of the array element in bytes: 
-2
Enter the index of the element of which you need address: 
3
Entered input is Invalid

Case-4

Enter the base address of the array : 
2000
Enter the size of the array element in bytes: 
2
Enter the index of the element of which you need address: 
-3
Entered input is Invalid

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: