SlideShare a Scribd company logo
At the end of the lesson, the student should be
able to:
 Declare and create arrays
 Access array elements
 Determine the number of elements in an
array
 Declare and create multidimensional arrays
 Describe how to use arrays to manage multiple
values in the same variable.




Suppose we have here three variables of type int
with different identifiers for each variable.
int number1;
int number2;
int number3;
number1 = 1;
number2 = 2;
number3 = 3;
As you can see, it seems like a tedious task in
order to just initialize and use the variables
especially if they are used for the same purpose.
In Java and other programming
languages, there is one capability wherein we
can use one variable to store a list of data
and manipulate them more efficiently. This
type of variable is called an array.
●
An array stores multiple data items of the
same data type, in a contiguous block of
memory, divided into a number of slots.
●


The Java programming language allows you
to group multiple values of the same type
(lists) using one-dimensional arrays. Arrays
are useful when you have related pieces of
data (such as the ages for several people),but
you do not want to create separate variables
to hold each piece of data
Array of int
0

Number:

1

2

0

1

2

Array of flowers


To declare an array, write the data type, followed by
a set of square brackets[], followed by the identifier
name.

Syntax: type [] array_identifier;

Where:
 The type represents the primitive data type or
object type for the values stored in the array.
 The [] informs the compiler that you are declaring
an array
 The array_identifier is the name that you are
assigning to refer to the array.

For example,
int []ages; or

int ages[];
After declaring, we must create the array and
specify its length with a constructor
statement.
Syntax: array_identifier = new type [length];
where:
 The array_identifier is the name you are
assigning to reference the array.
 The type represents the primitive data type or
object type for the value stored in the array.
 The length represent the size of the array.



Use the following code to instantiate an array
of char called status and an array of int called
ages.
status = new char [20];
ages = new int [5];


You can fill the contents of an array after you
have created the array. The syntax for setting
the values in an array is:

array_identifier[index] = value;

where:
 The array_identifier is the name you are



assigning to the array.
The index represents the location in the array
where the value will be placed.
The value is the value you are assigning to
index in the array.
ages[0]
ages[1]
ages[2]
ages[3]
ages[4]
ages:

0
19

=
=
=
=
=

19;
42;
92;
33;
46;
1
42

2
92

3
33

4
46
If you know the values you want in your array at the
time that you declare the array, you can declare,
instantiate, and set the values for an
Array object in the same line of code. The syntax for
this combined declaration, instantiation, initialization
of values is:


type [] array_identifier = {comma-separated_list_of_values_or_expressions};
where:
● The type represents the primitive data type or object type for the
values stored in the array.
● The [] informs the compiler that you are declaring an array.
● The array_identifier is the name you are assigning to the array.
● The {comma-separated_list_of_values_or_expressions}
represents a list of values you want to store in the array or a list of
expressions with results that will be stored in the array.
type [] array_identifier = {commaseparated_list_of_values_or_expressions};

where:
 The type represents the primitive data type or object
type for the values stored in the array.
 The [] informs the compiler that you are declaring an
array.
 The array_identifier is the name you are assigning to the

array.




The {commaseparated_list_of_values_or_expressions}
represents a list of values you want to store in the array
or a list of expressions with results that will be stored in
the array.
The following statement combines the
previous declaration, instantiation,
and initialization examples for the ages array


int [] ages = {19, 42, 92, 33, 46};
//creates an array of boolean variables with identifier
//results. This array contains 4 elements that are
//initialized to values {true, false, true, false}
boolean results[] = { true, false, true, false };
//creates an array of 4 double variables initialized
//to the values {100, 90, 80, 75};
double []grades = {100, 90, 80, 75};
//creates an array of Strings with identifier days and
//initialized. This array contains 7 elements
String days[]= {“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”,“Sun”};
Each element of an array is accessed using its
index. To access a value from the array, state the
array name and the index number for the element
(in braces []) on the right side of an assignment
operator.
The following code example demonstrates how to
set the value at a particular index in an array:
status[1] = 3
names[2] = “Johann";
ages[2] = 10;
prices[3] = 9.99F;
The following code example demonstrates
how to retrieve values from a particular index
in an array:
char s = status[1];
String name = names [2];
int age = ages[2];
double price = prices[3];


More Related Content

What's hot

Arrays
ArraysArrays
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
Muthukumaran Subramanian
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
Java Tokens
Java  TokensJava  Tokens
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Array
ArrayArray
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 

What's hot (20)

Arrays
ArraysArrays
Arrays
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Array and string
Array and stringArray and string
Array and string
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Java threads
Java threadsJava threads
Java threads
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 

Similar to Array lecture

Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Arrays
ArraysArrays
Arrays
ViniVini48
 
Array in C
Array in CArray in C
Array in C
adityas29
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsx
VijayKumarLokanadam
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptx
VijayKumarLokanadam
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Khan
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
KirubelWondwoson1
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
FolkAdonis
 
Array
ArrayArray
Arrays
ArraysArrays
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
soniya555961
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 

Similar to Array lecture (20)

Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Arrays
ArraysArrays
Arrays
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Array in C
Array in CArray in C
Array in C
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsx
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptx
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Array
ArrayArray
Array
 
2 arrays
2   arrays2   arrays
2 arrays
 
Arrays
ArraysArrays
Arrays
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays
ArraysArrays
Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 

Array lecture

  • 1.
  • 2. At the end of the lesson, the student should be able to:  Declare and create arrays  Access array elements  Determine the number of elements in an array  Declare and create multidimensional arrays  Describe how to use arrays to manage multiple values in the same variable.
  • 3.   Suppose we have here three variables of type int with different identifiers for each variable. int number1; int number2; int number3; number1 = 1; number2 = 2; number3 = 3; As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.
  • 4. In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array. ● An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots. ●
  • 5.  The Java programming language allows you to group multiple values of the same type (lists) using one-dimensional arrays. Arrays are useful when you have related pieces of data (such as the ages for several people),but you do not want to create separate variables to hold each piece of data
  • 7.  To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name. Syntax: type [] array_identifier; Where:  The type represents the primitive data type or object type for the values stored in the array.  The [] informs the compiler that you are declaring an array  The array_identifier is the name that you are assigning to refer to the array. For example, int []ages; or int ages[];
  • 8. After declaring, we must create the array and specify its length with a constructor statement. Syntax: array_identifier = new type [length]; where:  The array_identifier is the name you are assigning to reference the array.  The type represents the primitive data type or object type for the value stored in the array.  The length represent the size of the array. 
  • 9.  Use the following code to instantiate an array of char called status and an array of int called ages. status = new char [20]; ages = new int [5];
  • 10.  You can fill the contents of an array after you have created the array. The syntax for setting the values in an array is: array_identifier[index] = value; where:  The array_identifier is the name you are   assigning to the array. The index represents the location in the array where the value will be placed. The value is the value you are assigning to index in the array.
  • 12. If you know the values you want in your array at the time that you declare the array, you can declare, instantiate, and set the values for an Array object in the same line of code. The syntax for this combined declaration, instantiation, initialization of values is:  type [] array_identifier = {comma-separated_list_of_values_or_expressions}; where: ● The type represents the primitive data type or object type for the values stored in the array. ● The [] informs the compiler that you are declaring an array. ● The array_identifier is the name you are assigning to the array. ● The {comma-separated_list_of_values_or_expressions} represents a list of values you want to store in the array or a list of expressions with results that will be stored in the array.
  • 13. type [] array_identifier = {commaseparated_list_of_values_or_expressions}; where:  The type represents the primitive data type or object type for the values stored in the array.  The [] informs the compiler that you are declaring an array.  The array_identifier is the name you are assigning to the array.   The {commaseparated_list_of_values_or_expressions} represents a list of values you want to store in the array or a list of expressions with results that will be stored in the array.
  • 14. The following statement combines the previous declaration, instantiation, and initialization examples for the ages array  int [] ages = {19, 42, 92, 33, 46};
  • 15. //creates an array of boolean variables with identifier //results. This array contains 4 elements that are //initialized to values {true, false, true, false} boolean results[] = { true, false, true, false }; //creates an array of 4 double variables initialized //to the values {100, 90, 80, 75}; double []grades = {100, 90, 80, 75}; //creates an array of Strings with identifier days and //initialized. This array contains 7 elements String days[]= {“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”,“Sun”};
  • 16. Each element of an array is accessed using its index. To access a value from the array, state the array name and the index number for the element (in braces []) on the right side of an assignment operator. The following code example demonstrates how to set the value at a particular index in an array: status[1] = 3 names[2] = “Johann"; ages[2] = 10; prices[3] = 9.99F;
  • 17. The following code example demonstrates how to retrieve values from a particular index in an array: char s = status[1]; String name = names [2]; int age = ages[2]; double price = prices[3]; 