SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 
Array
ArrayArray
Array
Hajar
 

Was ist angesagt? (20)

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
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Visula C# Programming Lecture 5
Visula C# Programming Lecture 5Visula C# Programming Lecture 5
Visula C# Programming Lecture 5
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
2 arrays
2   arrays2   arrays
2 arrays
 
Data structures and algorithms arrays
Data structures and algorithms   arraysData structures and algorithms   arrays
Data structures and algorithms arrays
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Array lecture
Array lectureArray lecture
Array lecture
 
Array
ArrayArray
Array
 
Array in Java
Array in JavaArray in Java
Array in Java
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Array
ArrayArray
Array
 

Andere mochten auch (7)

6 plus 1 traits
6 plus 1 traits6 plus 1 traits
6 plus 1 traits
 
Sun, Moon and Planets Slideshow
Sun, Moon and Planets SlideshowSun, Moon and Planets Slideshow
Sun, Moon and Planets Slideshow
 
Punctuation marks
Punctuation marksPunctuation marks
Punctuation marks
 
Punctuation
PunctuationPunctuation
Punctuation
 
Punctuation
PunctuationPunctuation
Punctuation
 
Punctuation Powerpoint
Punctuation PowerpointPunctuation Powerpoint
Punctuation Powerpoint
 
The Solar System Powerpoint
The Solar System PowerpointThe Solar System Powerpoint
The Solar System Powerpoint
 

Ähnlich wie Arrays Java

Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
RhettB
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
mlrbrown
 

Ähnlich wie Arrays Java (20)

Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
Arrays
ArraysArrays
Arrays
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Eo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5eEo gaddis java_chapter_07_5e
Eo gaddis java_chapter_07_5e
 
Introduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptxIntroduction to Array & Structure & Basic Algorithms.pptx
Introduction to Array & Structure & Basic Algorithms.pptx
 
C
CC
C
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

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