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

What's hot (20)

PDF
An Introduction to Programming in Java: Arrays
Martin Chapman
 
PPTX
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
PPTX
Lesson 11 one dimensional array
MLG College of Learning, Inc
 
PPTX
Arrays in java
Arzath Areeff
 
PPT
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
PPT
02 c++ Array Pointer
Tareq Hasan
 
PDF
Arrays in python
moazamali28
 
PDF
Java Arrays
OXUS 20
 
PPTX
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
PPT
2 arrays
trixiacruz
 
PPTX
Data structures and algorithms arrays
chauhankapil
 
PDF
Java arrays (1)
Liza Abello
 
PPTX
Working with arrays in php
Kamal Acharya
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PPTX
Array lecture
Joan Saño
 
PPT
Array
Hajar
 
PPT
Array in Java
Shehrevar Davierwala
 
PPT
One Dimensional Array
dincyjain
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Array
Anil Neupane
 
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
LATHA LAKSHMI
 
Lesson 11 one dimensional array
MLG College of Learning, Inc
 
Arrays in java
Arzath Areeff
 
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
02 c++ Array Pointer
Tareq Hasan
 
Arrays in python
moazamali28
 
Java Arrays
OXUS 20
 
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
2 arrays
trixiacruz
 
Data structures and algorithms arrays
chauhankapil
 
Java arrays (1)
Liza Abello
 
Working with arrays in php
Kamal Acharya
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Array lecture
Joan Saño
 
Array
Hajar
 
Array in Java
Shehrevar Davierwala
 
One Dimensional Array
dincyjain
 
Arrays in Java
Naz Abdalla
 

Viewers also liked (7)

PPTX
6 plus 1 traits
CristinaSaraT
 
PPTX
Sun, Moon and Planets Slideshow
sanstanton
 
PPT
Punctuation marks
ewaszolek
 
PPTX
Punctuation
OKate321
 
PPT
Punctuation
theLecturette
 
PPTX
Punctuation Powerpoint
conno1ej
 
PPT
The Solar System Powerpoint
oliverh
 
6 plus 1 traits
CristinaSaraT
 
Sun, Moon and Planets Slideshow
sanstanton
 
Punctuation marks
ewaszolek
 
Punctuation
OKate321
 
Punctuation
theLecturette
 
Punctuation Powerpoint
conno1ej
 
The Solar System Powerpoint
oliverh
 
Ad

Similar to Arrays Java (20)

PPTX
Chap6java5th
Asfand Hassan
 
PPTX
Chapter 6 Absolute Java
Shariq Alee
 
PPTX
Upstate CSCI 200 Java Chapter 8 - Arrays
DanWooster1
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PDF
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
PPT
ch06.ppt
AqeelAbbas94
 
PPT
ch06.ppt
ansariparveen06
 
PPT
array Details
shivas379526
 
PPT
ch06.ppt
chandrasekar529044
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPT
Eo gaddis java_chapter_07_5e
Gina Bullock
 
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
PDF
javaarray
Arjun Shanka
 
PPTX
Arrays in programming
TaseerRao
 
PPT
ch11.ppt
kavitamittal18
 
PPT
Arrays in java programming language slides
ssuser5d6130
 
PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PDF
Arrays a detailed explanation and presentation
riazahamed37
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PPT
Cso gaddis java_chapter8
mlrbrown
 
Chap6java5th
Asfand Hassan
 
Chapter 6 Absolute Java
Shariq Alee
 
Upstate CSCI 200 Java Chapter 8 - Arrays
DanWooster1
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Java chapter 6 - Arrays -syntax and use
Mukesh Tekwani
 
ch06.ppt
AqeelAbbas94
 
ch06.ppt
ansariparveen06
 
array Details
shivas379526
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Eo gaddis java_chapter_07_5e
Gina Bullock
 
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
javaarray
Arjun Shanka
 
Arrays in programming
TaseerRao
 
ch11.ppt
kavitamittal18
 
Arrays in java programming language slides
ssuser5d6130
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Arrays a detailed explanation and presentation
riazahamed37
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Cso gaddis java_chapter8
mlrbrown
 
Ad

Recently uploaded (20)

PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

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