
Array Concepts using JAVA
Presentation
•
Science, Computers
•
University
•
Hard
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
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
Similar Resources on Wayground
6 questions
Cognitive behavioural therapy
Presentation
•
University
6 questions
Web Technologies
Presentation
•
University
6 questions
宇宙と季節
Presentation
•
KG
7 questions
PCIT21 WEEK 2
Presentation
•
University
7 questions
Python
Presentation
•
University
8 questions
multimedia1
Presentation
•
KG
6 questions
Week 3 CAD 154
Presentation
•
University
6 questions
WJM quiz
Presentation
•
University
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade