SlideShare a Scribd company logo
2
Most read
6
Most read
12
Most read
D.SEETHALAKSHMI
Assistant Professor
Department of Computer Applications
Bon Secours College for Women
Introduction
 Array: An ordered collection of values with two
distinguishing characters:
 Ordered and fixed length
 Homogeneous. Every value in the array must be of the same type
 The individual values in an array are called elements.
 The number of elements is called the length of the array
 Each element is identified by its position number in the
array, which is called index. In Java, the index numbers
begin with 0.
Array declaration
An array is characterized by
 Element type
 Length
type[ ] identifier = new type[length];
Default values in initialization
 numerics 0
 boolean false
 objects null
An array of objects
Elements of an array can be objects of any Java class.
Example: An array of 5 instances of the student class
Student[] topStudents = new Student[5];
Defining length
 Use named constant to declare the length of an array.
private static final in N_JUDGES = 5;
double[ ] scores = new double[N_JUDGES];
 Or read the length of an array from the user.
Selecting elements
Identifying an element
array[index]
 Index can be an expression
 Cycling through array elements
for (int i = 0; i < array.length; i++) {
operations involving the ith element
}
Human-readable index values
 From time to time, the fact that Java starts
index numbering at 0 can be confusing.
Sometimes, it makes sense to let the user work
with index numbers that begin with 1.
 Two standard ways:
1. Use Java’s index number internally and then add
one whenever those numbers are presented to the
user.
2. Use index values beginning at 1 and ignore the first
(0) element in each array. This strategy requires
allocating an additional element for each array but
has the advantage that the internal and external
index numbers correspond.
Internal representation of arrays
Student[] topStudents = new Student[2];
topStudents[0] = new Student(“Abcd”, 314159);
FFB8
FFBC
FFC0
1000
topStudents
stack
1000
1004
1008
100C
1010
length
topStudents[0]
topStudents[1]
2
null
null
heap
Student[] topStudents = new Student[2];
topStudents[0] = new Student(“Abcd”, 314159);
1000
1004
1008
100C
1010
1014
1018
101C
1020
1024
1028
102C
1030
1034
1038
103C
1040
4
A b
c d
1014
314159
0.0
false
2
1028
null
length
topStudents[0]
topStudents[1]
length
studentName
studentID
creditsEarned
paidUp
1000
topStudents FFB8
FFBC
FFC0
Passing arrays as parameters
 Recall: Passing objects (references) versus primitive
type (values) as parameters.
 Java defines all arrays as objects, implying that the
elements of an array are shared between the callee
and the caller.
swapElements(array[i], array[n – i – 1]) (wrong)
swapElements(array, i, n – i – 1)
private void swapElements(int[] array, int p1, int p2) {
int tmp = array[p1];
array[p1] = array[p2];
array[p2] = tmp;
}
 Every array in Java has a length field.
private void reverseArray(int[] array) {
for (int i = 0; i < array.length / 2; i++) {
swapElements(array, i, array.length – i – 1);
}
}
Using arrays
Example: Letter frequency table
 Design a data structure for the problem
Array: letterCounts[ ]
index: distance from ‘A’
index = Character.toUpperCase(ch) – ‘A’
letterCounts[0] is the count for ‘A’ or ‘a’
A convenient way of initializing an array:
int[ ] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
private static final String[ ] US_CITIES_OVER_ONE_MILLION = {
“New York”,
“Los Angeles”,
“Chicago”,
“Huston”,
“Philadelphia”,
“Phoenix”,
“San Diego”,
“San Antonio”,
“Dallas”,
}
Arrays and graphics
 Arrays turn up frequently in graphical programming. Any
time that you have repeated collections of similar objects, an
array provides a convenient structure for storing them.
 As an aesthetically pleasing illustration of both the use of
arrays and the possibility of creating dynamic pictures using
nothing but straight lines the text presents YarnPattern
program, which simulates the following process:
 Place a set of pegs at regular intervals around a rectangular border
 Tie a piece of colored yarn around the peg in the upper left corner
 Loop that yarn around that peg a certain distance DELTA ahead
 Continuing moving forward DELTA pegs until you close the loop
Two-dimensional arrays
Each element of an array is an array (of the same
dimension)
int[][] A = new int[3][2]
An array of three arrays of dimension two
A[0][0] A[0][1]
A[1][0] A[1][1]
A[2][0] A[2][0]
3-by-2 matrix
Memory allocation (row orientation)
A[0][0]
A[0][1]
A[1][0]
A[1][1]
A[2][0]
A[2][1]
Initializing a two-dimensional array
Static int A[3][2] = {
{1, 4},
{2, 5},
{3, 6}
};
A 3-by-2 matrix
The ArrayList Class
 Although arrays are conceptually important as a data
structure, they are not used as much in Java as they are in
most other languages. The reason is that the java.util
package includes a class called ArrayList that provides the
standard array behavior along with other useful operations.
 ArrayList is a Java class rather than a special form in the
language. As a result, all operations on ArrayLists are
indicated using method calls. For example,
 You create a new ArrayList by calling the ArrayList constructor.
 You get the number of elements by calling the size method rather than
by selecting a length field.
 You use the get and set methods to select individual elements.
Methods in the ArrayList class
Figure 11-12, p. 443, where <T> is the base type.
boolean add(<T> element)
<T> remove(int index)
int indexOf(<T> value)
An ArrayList allows you to add new elements to the end of a list.
By contrast, you can’t change the size of an existing array
without allocating a new array and then copying all the
elements from the old array into the new one.
Linking objects
 Objects in Java can contain references to other objects. Such
objects are said to be linked. Linked structures are used quite
often in programming.
 An integer list:
public class IntegerList {
public IntegerList(int n, IntegerList link) {
value = n;
next = link;
}
/* Private instance variables */
private int value;
private IntegerList next;
}
Linked structures
 Java defines a special value called null to represent a
reference to a nonexistent value and can be assigned to any
variable that holds an object reference. Thus you can assign
null to the next field of the last element to signify the end
of the list.
 You can insert or remove an element from a list. The size of a
linked structure can change. Also, elements of a linked
structure can be objects.
 A simple example: SignalTower class, Figure 7-3, p. 242.
Arrays vs. linked lists
 The two attributes that define a data type are: domain and a
set of operations.
 An array is a collection of items of the same type. It is
efficient to select an element. The addresses of array[i] is
the address of array + sizeof(overhead) +
i*sizeof(type). For example, if the type is int, then
sizeof(int) is 4. Since the array size is fixed, it is hard to
insert or delete an element.
 The items on a list can have different types. Linked lists can
represent general structures such as tree. Items can be inserted
to or removed from a list. However, to select an element, you
have to follow the links starting from the first item on the list
(sequential access).

More Related Content

What's hot (20)

DOC
Arrays and Strings
Dr.Subha Krishna
 
PPTX
Oracle sql analytic functions
mamamowebby
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Pointer in C++
Mauryasuraj98
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
PPTX
Constants in java
Manojkumar C
 
PDF
An Introduction to Programming in Java: Arrays
Martin Chapman
 
PPTX
Constructor overloading & method overloading
garishma bhatia
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PPT
Super and final in java
anshu_atri
 
PDF
Java threads
Prabhakaran V M
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Java Stack Data Structure.pptx
vishal choudhary
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
Arrays and Strings
Dr.Subha Krishna
 
Oracle sql analytic functions
mamamowebby
 
Classes objects in java
Madishetty Prathibha
 
Arrays in Java
Abhilash Nair
 
Pointer in C++
Mauryasuraj98
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Constants in java
Manojkumar C
 
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Constructor overloading & method overloading
garishma bhatia
 
Java abstract class & abstract methods
Shubham Dwivedi
 
9. Input Output in java
Nilesh Dalvi
 
Input and output in C++
Nilesh Dalvi
 
Network programming in java - PPT
kamal kotecha
 
Super and final in java
anshu_atri
 
Java threads
Prabhakaran V M
 
Arrays in Java
Naz Abdalla
 
Java Stack Data Structure.pptx
vishal choudhary
 
Polymorphism in java
Elizabeth alexander
 
Merge sort and quick sort
Shakila Mahjabin
 
Data Types & Variables in JAVA
Ankita Totala
 

Similar to Arrays in JAVA.ppt (20)

PPT
ch11.ppt
kavitamittal18
 
PPT
Arrays in java programming language slides
ssuser5d6130
 
PPTX
Arrays in programming
TaseerRao
 
DOCX
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
arraylist in java a comparison of the array and arraylist
PriyadharshiniG41
 
PPT
Ap Power Point Chpt6
dplunkett
 
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
PPT
Arrays in Java Programming Language slides
ssuser5d6130
 
PPT
17-Arrays en java presentación documento
DiegoGamboaSafla
 
PPT
Ppt chapter09
Richard Styner
 
PPS
Java session04
Niit Care
 
PPT
ch06.ppt
AqeelAbbas94
 
PPT
ch06.ppt
ansariparveen06
 
PPT
array Details
shivas379526
 
PPT
ch06.ppt
chandrasekar529044
 
PPT
Eo gaddis java_chapter_07_5e
Gina Bullock
 
PPT
STRINGS IN JAVA
LOVELY PROFESSIONAL UNIVERSITY
 
PPT
Array.ppt
SbsOmit1
 
ch11.ppt
kavitamittal18
 
Arrays in java programming language slides
ssuser5d6130
 
Arrays in programming
TaseerRao
 
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
arraylist in java a comparison of the array and arraylist
PriyadharshiniG41
 
Ap Power Point Chpt6
dplunkett
 
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
KierenReynolds3
 
Arrays in Java Programming Language slides
ssuser5d6130
 
17-Arrays en java presentación documento
DiegoGamboaSafla
 
Ppt chapter09
Richard Styner
 
Java session04
Niit Care
 
ch06.ppt
AqeelAbbas94
 
ch06.ppt
ansariparveen06
 
array Details
shivas379526
 
Eo gaddis java_chapter_07_5e
Gina Bullock
 
Array.ppt
SbsOmit1
 
Ad

More from SeethaDinesh (20)

PPTX
Input Devices.pptx
SeethaDinesh
 
PPT
Generations of Computers.ppt
SeethaDinesh
 
PPT
Inheritance in java.ppt
SeethaDinesh
 
PPTX
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
PPTX
Cloud Computing Basics.pptx
SeethaDinesh
 
PPT
unit 5 stack & queue.ppt
SeethaDinesh
 
PPT
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
PPT
Structure and Union.ppt
SeethaDinesh
 
DOCX
Shortest Path Problem.docx
SeethaDinesh
 
PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
PPT
NME UNIT II.ppt
SeethaDinesh
 
PDF
DS UNIT 1.pdf
SeethaDinesh
 
PPT
Basics of C.ppt
SeethaDinesh
 
PPTX
chapter 1.pptx
SeethaDinesh
 
PPTX
DW unit 3.pptx
SeethaDinesh
 
PDF
DW unit 2.pdf
SeethaDinesh
 
PDF
DW Unit 1.pdf
SeethaDinesh
 
PPTX
NME WPI UNIt 3.pptx
SeethaDinesh
 
PDF
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
PDF
graphtraversals.pdf
SeethaDinesh
 
Input Devices.pptx
SeethaDinesh
 
Generations of Computers.ppt
SeethaDinesh
 
Inheritance in java.ppt
SeethaDinesh
 
PROGRAMMING IN JAVA unit 1.pptx
SeethaDinesh
 
Cloud Computing Basics.pptx
SeethaDinesh
 
unit 5 stack & queue.ppt
SeethaDinesh
 
Greedy_Backtracking graph coloring.ppt
SeethaDinesh
 
Structure and Union.ppt
SeethaDinesh
 
Shortest Path Problem.docx
SeethaDinesh
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
NME UNIT II.ppt
SeethaDinesh
 
DS UNIT 1.pdf
SeethaDinesh
 
Basics of C.ppt
SeethaDinesh
 
chapter 1.pptx
SeethaDinesh
 
DW unit 3.pptx
SeethaDinesh
 
DW unit 2.pdf
SeethaDinesh
 
DW Unit 1.pdf
SeethaDinesh
 
NME WPI UNIt 3.pptx
SeethaDinesh
 
NME UNIT I & II MATERIAL.pdf
SeethaDinesh
 
graphtraversals.pdf
SeethaDinesh
 
Ad

Recently uploaded (20)

PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 

Arrays in JAVA.ppt

  • 1. D.SEETHALAKSHMI Assistant Professor Department of Computer Applications Bon Secours College for Women
  • 2. Introduction  Array: An ordered collection of values with two distinguishing characters:  Ordered and fixed length  Homogeneous. Every value in the array must be of the same type  The individual values in an array are called elements.  The number of elements is called the length of the array  Each element is identified by its position number in the array, which is called index. In Java, the index numbers begin with 0.
  • 3. Array declaration An array is characterized by  Element type  Length type[ ] identifier = new type[length]; Default values in initialization  numerics 0  boolean false  objects null
  • 4. An array of objects Elements of an array can be objects of any Java class. Example: An array of 5 instances of the student class Student[] topStudents = new Student[5];
  • 5. Defining length  Use named constant to declare the length of an array. private static final in N_JUDGES = 5; double[ ] scores = new double[N_JUDGES];  Or read the length of an array from the user.
  • 6. Selecting elements Identifying an element array[index]  Index can be an expression  Cycling through array elements for (int i = 0; i < array.length; i++) { operations involving the ith element }
  • 7. Human-readable index values  From time to time, the fact that Java starts index numbering at 0 can be confusing. Sometimes, it makes sense to let the user work with index numbers that begin with 1.  Two standard ways: 1. Use Java’s index number internally and then add one whenever those numbers are presented to the user. 2. Use index values beginning at 1 and ignore the first (0) element in each array. This strategy requires allocating an additional element for each array but has the advantage that the internal and external index numbers correspond.
  • 8. Internal representation of arrays Student[] topStudents = new Student[2]; topStudents[0] = new Student(“Abcd”, 314159); FFB8 FFBC FFC0 1000 topStudents stack 1000 1004 1008 100C 1010 length topStudents[0] topStudents[1] 2 null null heap
  • 9. Student[] topStudents = new Student[2]; topStudents[0] = new Student(“Abcd”, 314159); 1000 1004 1008 100C 1010 1014 1018 101C 1020 1024 1028 102C 1030 1034 1038 103C 1040 4 A b c d 1014 314159 0.0 false 2 1028 null length topStudents[0] topStudents[1] length studentName studentID creditsEarned paidUp 1000 topStudents FFB8 FFBC FFC0
  • 10. Passing arrays as parameters  Recall: Passing objects (references) versus primitive type (values) as parameters.  Java defines all arrays as objects, implying that the elements of an array are shared between the callee and the caller. swapElements(array[i], array[n – i – 1]) (wrong) swapElements(array, i, n – i – 1)
  • 11. private void swapElements(int[] array, int p1, int p2) { int tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }  Every array in Java has a length field. private void reverseArray(int[] array) { for (int i = 0; i < array.length / 2; i++) { swapElements(array, i, array.length – i – 1); } }
  • 12. Using arrays Example: Letter frequency table  Design a data structure for the problem Array: letterCounts[ ] index: distance from ‘A’ index = Character.toUpperCase(ch) – ‘A’ letterCounts[0] is the count for ‘A’ or ‘a’
  • 13. A convenient way of initializing an array: int[ ] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; private static final String[ ] US_CITIES_OVER_ONE_MILLION = { “New York”, “Los Angeles”, “Chicago”, “Huston”, “Philadelphia”, “Phoenix”, “San Diego”, “San Antonio”, “Dallas”, }
  • 14. Arrays and graphics  Arrays turn up frequently in graphical programming. Any time that you have repeated collections of similar objects, an array provides a convenient structure for storing them.  As an aesthetically pleasing illustration of both the use of arrays and the possibility of creating dynamic pictures using nothing but straight lines the text presents YarnPattern program, which simulates the following process:  Place a set of pegs at regular intervals around a rectangular border  Tie a piece of colored yarn around the peg in the upper left corner  Loop that yarn around that peg a certain distance DELTA ahead  Continuing moving forward DELTA pegs until you close the loop
  • 15. Two-dimensional arrays Each element of an array is an array (of the same dimension) int[][] A = new int[3][2] An array of three arrays of dimension two A[0][0] A[0][1] A[1][0] A[1][1] A[2][0] A[2][0] 3-by-2 matrix
  • 16. Memory allocation (row orientation) A[0][0] A[0][1] A[1][0] A[1][1] A[2][0] A[2][1]
  • 17. Initializing a two-dimensional array Static int A[3][2] = { {1, 4}, {2, 5}, {3, 6} }; A 3-by-2 matrix
  • 18. The ArrayList Class  Although arrays are conceptually important as a data structure, they are not used as much in Java as they are in most other languages. The reason is that the java.util package includes a class called ArrayList that provides the standard array behavior along with other useful operations.  ArrayList is a Java class rather than a special form in the language. As a result, all operations on ArrayLists are indicated using method calls. For example,  You create a new ArrayList by calling the ArrayList constructor.  You get the number of elements by calling the size method rather than by selecting a length field.  You use the get and set methods to select individual elements.
  • 19. Methods in the ArrayList class Figure 11-12, p. 443, where <T> is the base type. boolean add(<T> element) <T> remove(int index) int indexOf(<T> value) An ArrayList allows you to add new elements to the end of a list. By contrast, you can’t change the size of an existing array without allocating a new array and then copying all the elements from the old array into the new one.
  • 20. Linking objects  Objects in Java can contain references to other objects. Such objects are said to be linked. Linked structures are used quite often in programming.  An integer list: public class IntegerList { public IntegerList(int n, IntegerList link) { value = n; next = link; } /* Private instance variables */ private int value; private IntegerList next; }
  • 21. Linked structures  Java defines a special value called null to represent a reference to a nonexistent value and can be assigned to any variable that holds an object reference. Thus you can assign null to the next field of the last element to signify the end of the list.  You can insert or remove an element from a list. The size of a linked structure can change. Also, elements of a linked structure can be objects.  A simple example: SignalTower class, Figure 7-3, p. 242.
  • 22. Arrays vs. linked lists  The two attributes that define a data type are: domain and a set of operations.  An array is a collection of items of the same type. It is efficient to select an element. The addresses of array[i] is the address of array + sizeof(overhead) + i*sizeof(type). For example, if the type is int, then sizeof(int) is 4. Since the array size is fixed, it is hard to insert or delete an element.  The items on a list can have different types. Linked lists can represent general structures such as tree. Items can be inserted to or removed from a list. However, to select an element, you have to follow the links starting from the first item on the list (sequential access).