0% found this document useful (0 votes)
5 views17 pages

Arrays

The document provides an overview of Java arrays, describing them as collections of similar data types that are indexed and occupy contiguous memory locations. It discusses the advantages and disadvantages of arrays, types of arrays (single and multidimensional), and includes syntax for declaration, instantiation, and initialization. Additionally, it covers passing arrays to methods, returning arrays, and handling exceptions related to array indexing.

Uploaded by

aliomiye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Arrays

The document provides an overview of Java arrays, describing them as collections of similar data types that are indexed and occupy contiguous memory locations. It discusses the advantages and disadvantages of arrays, types of arrays (single and multidimensional), and includes syntax for declaration, instantiation, and initialization. Additionally, it covers passing arrays to methods, returning arrays, and handling exceptions related to array indexing.

Uploaded by

aliomiye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Section-8

Java Arrays

Eng. Abdirisak Abdalla


JacfuuTechno
Department of computer science
1
Java Arrays
 An array is a collection of similar data types.
 Array is a container object that hold values of
homogeneous type.
 It is also known as static data structure because size
of an array must be specified at the time of its
declaration.
 Array starts from zero index and goes to n-1 where
n is length of the array.
 In Java, array is treated as an object and stores
into heap memory. It allows to store primitive
values or reference values. JacfuuTechno
2
Cont..
 Advantages
 Code Optimization: It makes the code optimized,
we can retrieve or sort the data efficiently.
 Random access: We can get any data located at
an index position.
 Disadvantages
 Size Limit: We can store only the fixed size of
elements in the array.

JacfuuTechno
3
Features of Array
 It is always indexed. Index begins from 0.
 It is a collection of similar data types.
 It occupies a contiguous memory location.
 It allows to access elements randomly.

JacfuuTechno
4
Types of Array in java
 There are two types of array.
 Single Dimensional Array
 Multidimensional Array

JacfuuTechno
5
Single Dimensional Array
 A single dimensional array in Java is an array that
holds a sequence of elements, all of the same
type, accessed through an index.
 For instance, if we create an array to store integer data
type values, all elements of the array must be integers,
and they are accessed using their index positions.

JacfuuTechno
6
Syntax to Declare an Array in Java
 dataType[] arr; (or)
 dataType arr[];
 Instantiation of an Array in java
 arry1 RefVar=new datatype[size];

JacfuuTechno
7
Example of Java Array
 class Testarray{
 public static void main(String args[]){
 int a[]=new int[5];
 a[0]=10;
 a[1]=20; a[2]=70; a[3]=40; a[4]=50;
 for(int i=0;i<a.length;i++)
 System.out.println(a[i]); }}
 Note:-To find the length of an array, we can use length
property of array object like: array_name.length.
JacfuuTechno
8
Declaration, Instantiation and Initialization of Java Array
 We can declare, instantiate and initialize the java
array together by:int a[]={33,3,4,5};
 Example:
 class Testarray1{
 public static void main(String args[]){
 int a[]={33,3,4,5};
 for(int i=0;i<a.length;i++)
 System.out.println(a[i]); }}

JacfuuTechno
9
Passing Array to a Method in Java
 We can pass the java array to method so that we
can reuse the same logic on any array.

JacfuuTechno
10
Returning Array from the Method
 class TestReturnArray{
 static int[] get(){
 return new int[]{10,30,50,90,60}; }
 public static void main(String args[]){
 int arr[]=get();
 for(int i=0;i<arr.length;i++)
 System.out.println(arr[i]); }}

JacfuuTechno
11
ArrayIndexOutOfBoundsException
 The Java Virtual Machine (JVM) throws an
ArrayIndexOutOfBoundsException if length of the
array is negative, equal to the array size or greater
than the array size while traversing the array.
 public class TestArrayException{
 public static void main(String args[]){
 int arr[]={50,60,70,80};
 for(int i=0;i<=arr.length;i++){
 System.out.println(arr[i]); } }}
JacfuuTechno
12
Multi-Dimensional Array
 A multi-dimensional array is very much similar to a
single dimensional array.
 It can have multiple rows and multiple columns
unlike single dimensional array, which can have only
one row index.
 It represent data into tabular form in which data is
stored into row and columns.

JacfuuTechno
13
Multi-Dimensional Array Declaration & Initialization
 datatype[ ][ ] arrayName;
 Initialization of Array
 datatype[ ][ ] arrayName = new
int[no_of_rows][no_of_columns];

JacfuuTechno
14
Example:
 class Testarray3{
 public static void main(String args[]){
 int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
 //printing 2D array
 for(int i=0;i<3;i++){
 for(int j=0;j<3;j++){
 System.out.print(arr[i][j]+" ");
 }
 System.out.println();
 }
 }}
JacfuuTechno
15
Any Question?

16
Thank You

17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy