SlideShare a Scribd company logo
1
Object Oriented Programming
FIEC ESPOL
Arrays
Creating and Accessing Arrays
• An array can be created using one statement
double[] score = new double[5];
• Or using two statements:
double[] score;double[] score;
score = new double[5];
– The first statement declares score an array of
doubles
– The second statement creates an array and makes
the variable score a name for the array
2
Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
Declaring and Creating an Array
• An array is declared and created in almost the
same way that objects are declared and created:
BaseType[] ArrayName = new BaseType[size];
– The size may be given as an expression that
l t t ti i t f levaluates to a nonnegative integer, for example, an
int variable
char[] line = new char[80];
double[] reading = new double[count];
Person[] specimen = new Person[100];
3
Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
Three Ways to Use Square Brackets [] with
an Array Name
• Square brackets can be used to create a type
name:
double[] score;
• Square brackets can be used with an integerq g
value as part of the special syntax Java uses to
create a new array:
score = new double[5];
• Square brackets can be used to name an
indexed variable of an array:
max = score[0];
4
Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
2
The length Instance Variable
• An array is considered to be an object
• Every array has exactly one instance variable
named length
d bl [] di d bl [100]
5Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
double[] reading = new double[100];
for (int index = 0; index < reading.length; index++)
{
reading[index] = 42.0;
}
An Array of Characters Is Not a String
• The class String has a constructor that has a
single parameter of type char[]
String s = new String(a);
– The object s will have the same sequence of
characters as the entire array a ("ABC"), but is ancharacters as the entire array a ( ABC ), but is an
independent copy
• Another String constructor uses a subrange of
a character array instead
String s2 = new String(a,0,2);
– Given a as before, the new string object is "AB“
(continued)
6Copyright © 2008 Pearson Addison‐Wesley.
All rights reserved
An Array of Characters Is Not a String
• An array of characters does have some
things in common with String objects
– For example, an array of characters can be
output using println
System.out.println(a);
– Given a as before, this would produce the
output
ABC
7
Copyright © 2008 Pearson Addison‐Wesley.
All rights reserved
Arrays and References
• Like class types, a variable of an array
type holds a reference
– Arrays are objects
A variable of an array type holds the address– A variable of an array type holds the address
of where the array object is stored in memory
– Array types are (usually) considered to be
class types
8Copyright © 2008 Pearson Addison‐Wesley
. All rights reserved
3
Arrays with a Class Base Type
• The base type of an array can be a class type
Date[] holidayList = new Date[20];
• The above example creates 20 indexed
reference variables of type Date. It does notyp
create 20 objects of the class Date
– Each of these indexed variables are automatically
initialized to null
– Any attempt to reference any them at this point would
result in a "null pointer exception" error message
(continued)
9Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
Arrays with a Class Base Type
• Like any other object, each of the indexed variables
requires a separate invocation of a constructor using
new (singly, or perhaps using a for loop) to create an
object to reference
holidayList[0] = new Date();y [ ] ();
. . .
holidayList[19] = new Date();
OR
for (int i = 0; i < holidayList.length; i++)
holidayList[i] = new Date();
• Each of the indexed variables can now be referenced
since each holds the memory address of a Date object
10Copyright © 2008 Pearson Addison‐Wesley.
All rights reserved
Arguments for the Method main
• The heading for the main method of a program
has a parameter for an array of String
– It is usually called args by convention
public static void main(String[] args)
– Note that since args is a parameter it could be
11
Note that since args is a parameter, it could be
replaced by any other non-keyword identifier
• If a Java program is run without giving an
argument to main, then a default empty array of
strings is automatically provided
Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
Arguments for the Method main
• If a program requires that the main
method be provided an array of strings
argument, each element must be provided
from the command line when the program
12
from the command line when the program
is run
java SomeProgram Hi ! there
– This will set args[0] to "Hi", args[1] to "!",
and args[2] to "there"
– It will also set args.length to 3
Copyright © 2008 Pearson Addison‐Wesley
All rights reserved
4
Multidimensional Arrays
• It is sometimes useful to have an array with
more than one index
• Multidimensional arrays are declared and
created in basically the same way as one-
dimensional arrays
Aug 6, 2007 16
y
– You simply use as many square brackets as there are
indices
– Each index must be enclosed in its own brackets
double[][]table = new double[100][10];
int[][][] figure = new int[10][20][30];
Person[][] = new Person[10][100];
Copyright © 2008 Pearson Addison‐Wesley. 
All rights reserved
Multidimensional Arrays
• Multidimensional arrays may have any number
of indices, but perhaps the most common
number is two
– Two-dimensional array can be visualized as a two-
dimensional display with the first index giving the row
Aug 6, 2007 17
dimensional display with the first index giving the row,
and the second index giving the column
char[][] a = new char[5][12];
– Note that, like a one-dimensional array, each element
of a multidimensional array is just a variable of the
base type (in this case, char)
Copyright © 2008 Pearson Addison‐Wesley.
All rights reserved
Using the length Instance Variable
• The following program demonstrates how a
nested for loop can be used to process a
two-dimensional array
– Note how each length instance variable is used
int row, column;
for (row = 0; row < page.length; row++)
for (column = 0; column < page[row].length;
column++)
page[row][column] = 'Z';
Copyright © 2008 Pearson Addison‐Wesley.
All rights reserved

More Related Content

PDF
Lecture 6 - Arrays
PDF
Array in Java
PDF
Week06
PPTX
Java arrays
PPTX
Pi j3.4 data-structures
PPTX
Module 7 : Arrays
PPTX
Array in Java
Lecture 6 - Arrays
Array in Java
Week06
Java arrays
Pi j3.4 data-structures
Module 7 : Arrays
Array in Java

What's hot (20)

PDF
An Introduction to Programming in Java: Arrays
PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
PPTX
Lesson 11 one dimensional array
PPTX
Arrays in java
PPT
Visula C# Programming Lecture 5
PPT
02 c++ Array Pointer
PDF
Arrays in python
PDF
Java Arrays
PPTX
Computer programming 2 Lesson 13
PPT
2 arrays
PPTX
Data structures and algorithms arrays
PDF
Java arrays (1)
PPTX
Working with arrays in php
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Array lecture
PPT
Array
PPT
Array in Java
PPT
One Dimensional Array
PDF
Arrays in Java
PPTX
An Introduction to Programming in Java: Arrays
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Lesson 11 one dimensional array
Arrays in java
Visula C# Programming Lecture 5
02 c++ Array Pointer
Arrays in python
Java Arrays
Computer programming 2 Lesson 13
2 arrays
Data structures and algorithms arrays
Java arrays (1)
Working with arrays in php
Arrays In Python | Python Array Operations | Edureka
Array lecture
Array
Array in Java
One Dimensional Array
Arrays in Java
Ad

Viewers also liked (7)

PPTX
6 plus 1 traits
PPTX
Sun, Moon and Planets Slideshow
PPT
Punctuation marks
PPTX
Punctuation
PPT
Punctuation
PPTX
Punctuation Powerpoint
PPT
The Solar System Powerpoint
6 plus 1 traits
Sun, Moon and Planets Slideshow
Punctuation marks
Punctuation
Punctuation
Punctuation Powerpoint
The Solar System Powerpoint
Ad

Similar to Arrays Java (20)

PPTX
Chapter 6 Absolute Java
PPTX
Chap6java5th
PPT
Cso gaddis java_chapter8
PPT
Cso gaddis java_chapter8
PPT
ch06.ppt
PPT
PPT
ch06.ppt
PPT
array Details
PDF
M251_Meeting 2_updated_2.pdf(M251_Meeting 2_updated_2.pdf)
PPT
Basics of Data structure using C describing basics concepts
PPTX
Chapter-Five.pptx
PPTX
DOC-20240812-WA0000 array string and.pptx
PDF
Learn C# Programming - Nullables & Arrays
PPTX
Unit-2.Arrays and Strings.pptx.................
PPT
Arrays Basicfundamentaldatastructure.ppt
PPTX
Generative Coding Lecture notes using coding
PPTX
Arrays in programming
PPTX
array is a fundamental data structure that stores a collection of elements.pptx
PPT
Arrays JavaScript presentacion en power point
Chapter 6 Absolute Java
Chap6java5th
Cso gaddis java_chapter8
Cso gaddis java_chapter8
ch06.ppt
ch06.ppt
array Details
M251_Meeting 2_updated_2.pdf(M251_Meeting 2_updated_2.pdf)
Basics of Data structure using C describing basics concepts
Chapter-Five.pptx
DOC-20240812-WA0000 array string and.pptx
Learn C# Programming - Nullables & Arrays
Unit-2.Arrays and Strings.pptx.................
Arrays Basicfundamentaldatastructure.ppt
Generative Coding Lecture notes using coding
Arrays in programming
array is a fundamental data structure that stores a collection of elements.pptx
Arrays JavaScript presentacion en power point

Recently uploaded (20)

PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Spectroscopy.pptx food analysis technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
Assigned Numbers - 2025 - Bluetooth® Document
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
A comparative analysis of optical character recognition models for extracting...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
Building Integrated photovoltaic BIPV_UPV.pdf
A Presentation on Artificial Intelligence
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Spectroscopy.pptx food analysis technology
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Network Security Unit 5.pdf for BCA BBA.

Arrays Java

  • 1. 1 Object Oriented Programming FIEC ESPOL Arrays Creating and Accessing Arrays • An array can be created using one statement double[] score = new double[5]; • Or using two statements: double[] score;double[] score; score = new double[5]; – The first statement declares score an array of doubles – The second statement creates an array and makes the variable score a name for the array 2 Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved Declaring and Creating an Array • An array is declared and created in almost the same way that objects are declared and created: BaseType[] ArrayName = new BaseType[size]; – The size may be given as an expression that l t t ti i t f levaluates to a nonnegative integer, for example, an int variable char[] line = new char[80]; double[] reading = new double[count]; Person[] specimen = new Person[100]; 3 Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved Three Ways to Use Square Brackets [] with an Array Name • Square brackets can be used to create a type name: double[] score; • Square brackets can be used with an integerq g value as part of the special syntax Java uses to create a new array: score = new double[5]; • Square brackets can be used to name an indexed variable of an array: max = score[0]; 4 Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved
  • 2. 2 The length Instance Variable • An array is considered to be an object • Every array has exactly one instance variable named length d bl [] di d bl [100] 5Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved double[] reading = new double[100]; for (int index = 0; index < reading.length; index++) { reading[index] = 42.0; } An Array of Characters Is Not a String • The class String has a constructor that has a single parameter of type char[] String s = new String(a); – The object s will have the same sequence of characters as the entire array a ("ABC"), but is ancharacters as the entire array a ( ABC ), but is an independent copy • Another String constructor uses a subrange of a character array instead String s2 = new String(a,0,2); – Given a as before, the new string object is "AB“ (continued) 6Copyright © 2008 Pearson Addison‐Wesley. All rights reserved An Array of Characters Is Not a String • An array of characters does have some things in common with String objects – For example, an array of characters can be output using println System.out.println(a); – Given a as before, this would produce the output ABC 7 Copyright © 2008 Pearson Addison‐Wesley. All rights reserved Arrays and References • Like class types, a variable of an array type holds a reference – Arrays are objects A variable of an array type holds the address– A variable of an array type holds the address of where the array object is stored in memory – Array types are (usually) considered to be class types 8Copyright © 2008 Pearson Addison‐Wesley . All rights reserved
  • 3. 3 Arrays with a Class Base Type • The base type of an array can be a class type Date[] holidayList = new Date[20]; • The above example creates 20 indexed reference variables of type Date. It does notyp create 20 objects of the class Date – Each of these indexed variables are automatically initialized to null – Any attempt to reference any them at this point would result in a "null pointer exception" error message (continued) 9Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved Arrays with a Class Base Type • Like any other object, each of the indexed variables requires a separate invocation of a constructor using new (singly, or perhaps using a for loop) to create an object to reference holidayList[0] = new Date();y [ ] (); . . . holidayList[19] = new Date(); OR for (int i = 0; i < holidayList.length; i++) holidayList[i] = new Date(); • Each of the indexed variables can now be referenced since each holds the memory address of a Date object 10Copyright © 2008 Pearson Addison‐Wesley. All rights reserved Arguments for the Method main • The heading for the main method of a program has a parameter for an array of String – It is usually called args by convention public static void main(String[] args) – Note that since args is a parameter it could be 11 Note that since args is a parameter, it could be replaced by any other non-keyword identifier • If a Java program is run without giving an argument to main, then a default empty array of strings is automatically provided Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved Arguments for the Method main • If a program requires that the main method be provided an array of strings argument, each element must be provided from the command line when the program 12 from the command line when the program is run java SomeProgram Hi ! there – This will set args[0] to "Hi", args[1] to "!", and args[2] to "there" – It will also set args.length to 3 Copyright © 2008 Pearson Addison‐Wesley All rights reserved
  • 4. 4 Multidimensional Arrays • It is sometimes useful to have an array with more than one index • Multidimensional arrays are declared and created in basically the same way as one- dimensional arrays Aug 6, 2007 16 y – You simply use as many square brackets as there are indices – Each index must be enclosed in its own brackets double[][]table = new double[100][10]; int[][][] figure = new int[10][20][30]; Person[][] = new Person[10][100]; Copyright © 2008 Pearson Addison‐Wesley.  All rights reserved Multidimensional Arrays • Multidimensional arrays may have any number of indices, but perhaps the most common number is two – Two-dimensional array can be visualized as a two- dimensional display with the first index giving the row Aug 6, 2007 17 dimensional display with the first index giving the row, and the second index giving the column char[][] a = new char[5][12]; – Note that, like a one-dimensional array, each element of a multidimensional array is just a variable of the base type (in this case, char) Copyright © 2008 Pearson Addison‐Wesley. All rights reserved Using the length Instance Variable • The following program demonstrates how a nested for loop can be used to process a two-dimensional array – Note how each length instance variable is used int row, column; for (row = 0; row < page.length; row++) for (column = 0; column < page[row].length; column++) page[row][column] = 'Z'; Copyright © 2008 Pearson Addison‐Wesley. All rights reserved