0% found this document useful (0 votes)
11 views15 pages

Lecture9 Arrays in Java 1

Uploaded by

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

Lecture9 Arrays in Java 1

Uploaded by

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

Arrays in Java

Lecture - 9

Prepared By - Rupali Patil

10/26/2024 Object Oriented Programming 1


Contents

• Arrays in Java

• Array initialization
Vishwakarma Institute of Technology

• Default Array values

• Multi dimensional array

Object Oriented Programming 2


Arrays in Java

• An array is a collection of homogeneous elements that are


referred to by a common name.
• In Java all arrays are dynamically allocated.
Vishwakarma Institute of Technology

• Since arrays are objects in Java, we can find their length


using the object property length.
• The direct superclass of an array type is object.
• Arrays can be created for primitive as well as non primitive
data types.
Object Oriented Programming 3
Arrays in Java

• One-Dimensional Arrays
• A one-dimensional array is, essentially, a list of like-typed
variables.
Vishwakarma Institute of Technology

• To create an array, we first must create an array variable of


the desired type.
• The general form of a one-dimensional array declaration is
• type var-name[ ];
• Here, type declares the element type (also called the base
type) of the array. Object Oriented Programming 4
Arrays in Java

• The element type determines the data type of each


element that comprises the array.
• Thus, the element type for the array determines what type
Vishwakarma Institute of Technology

of data the array will hold.


• For example, the following declares an array named
month_days with the type “array of int”:
• int month_days[];

Object Oriented Programming 5


Arrays in Java

• Although this declaration establishes the fact that


month_days is an array variable, no array actually exists.
• To link month_days with an actual, physical array of
Vishwakarma Institute of Technology

integers, we must allocate one using new and assign it to


month_days.

• new is a special operator that allocates memory.

Object Oriented Programming 6


Arrays in Java

• The general form of new as it applies to one-


dimensional arrays appears as follows:
Vishwakarma Institute of Technology

• array-var = new type [size];

• type specifies the type of data being allocated.

• size specifies the number of elements in the array.

• array-var is the array variable that is linked to the


array.
Object Oriented Programming 7
Arrays in Java

• To use new to allocate an array, we must specify the type and


number of elements to allocate.
• The elements in the array allocated by new will automatically be
Vishwakarma Institute of Technology

initialized to default values.


• month_days = new int[12];

• This example allocates a 12 -element array of integers and links


them to month_days:
• After this statement executes, month_days will refer to an array
of 12 integers.
• Further, all elements in the array will be initialized to zero.
Object Oriented Programming 8
Array Declaration and Initialization

• Using literals

e. g. int [] x = {1,2,3,4,5,6,7,8,9,10};
• Using new keyword
Vishwakarma Institute of Technology

e.g int [] x = new int[10];

Object Oriented Programming 9


Array Declaration and Initialization contd..

• Example of array declaration, initialization


Vishwakarma Institute of Technology

Object Oriented Programming 10


Vishwakarma Institute of Technology
Default Array values

Object Oriented Programming 11


Types of Arrays
• Single Dimensional Array(1D)
• Multi Dimensional Array
Vishwakarma Institute of Technology

Object Oriented Programming 12


Vishwakarma Institute of Technology
Two Dimensional Array

Object Oriented Programming 13


Vishwakarma Institute of Technology
Three Dimensional Array

Object Oriented Programming 14


Thank You

10/26/2024 Object Oriented Programming 15

You might also like