Search Header Logo
Array Concepts using JAVA

Array Concepts using JAVA

Assessment

Presentation

Science, Computers

University

Hard

Created by

Suganthi Duraiswamy

Used 10+ times

FREE Resource

10 Slides • 0 Questions

1

Array Concepts using JAVA

by Dr. D. Suganthi, AP/MCA

​SRM University, Ramapuram

2

Array Definition

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.

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets []

3

Array Definition

  • The variables in the array are ordered and each have an index beginning from 0.

  • Java array can be also be used as a static field, a local variable or a method parameter.

  • The size of an array must be specified by an int or short value and not long.

  • The direct superclass of an array type is Object.

  • Every array type implements the interfaces Cloneable and java.io.Serializable.

4

Array Concepts using JAVA

The 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.

5

Array Representation

media

6

Single Dimensional Array

Creating, Initializing, and Accessing an Array:

One-Dimensional Arrays : The general form of a one-dimensional array declaration is

int intArray[]; //declaring array

intArray = new int[20]; // allocating memory to array (​or)

int[] intArray = new int[20]; // combining both statements in one

7

Array Literal

where the size of the array and variables of array are already known, array literals can be used.

int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal

8

Accessing Java Array Elements using for Loop

Each element in the array is accessed via its index. The index begins with 0 and ends at (total array size)-1. All the elements of array can be accessed using Java for Loop.

// accessing the elements of the specified array

for (int i = 0; i < arr.length; i++)

System.out.println("Element at index " + i + " : "+ arr[i]);

9

Simple Array program using JAVA

class GFG  {

    public static void main (String[] args)      {         

      // declares an Array of integers.

      int[] arr;        

      // allocating memory for 5 integers.

      arr = new int[5];           

      // initialize the first elements of the array

      arr[0] = 10;           

      // initialize the second elements of the array

      arr[1] = 20; //so on... arr[2] = 30; arr[3] = 40;   arr[4] = 50;

10

Simple Array program using JAVA

 // accessing the elements of the specified array

      for (int i = 0; i < arr.length; i++)

         System.out.println("Element at index " + i + 

                                      " : "+ arr[i]);          

    } }

Output:

Element at index 0 : 10

Element at index 1 : 20

Element at index 2 : 30

Element at index 3 : 40

Element at index 4 : 50

Array Concepts using JAVA

by Dr. D. Suganthi, AP/MCA

​SRM University, Ramapuram

Show answer

Auto Play

Slide 1 / 10

SLIDE

Discover more resources for Science