SlideShare ist ein Scribd-Unternehmen logo
1 von 83
Java Programming: From Problem Analysis to Program Design, 4e Chapter 9 Arrays
Chapter Objectives ,[object Object],[object Object],[object Object],[object Object]
Chapter Objectives (continued) ,[object Object],[object Object],[object Object]
Array ,[object Object],[object Object],[object Object],[object Object]
One-Dimensional Arrays
One-Dimensional Arrays (continued)
One-Dimensional Arrays (continued) ,[object Object],[object Object]
[object Object],Arrays
[object Object],Arrays (continued)
Array List
Array List (continued)
Array List (continued)
Array List (continued)
[object Object]
[object Object],[object Object],Array Initialization During Declaration
Array Initialization During Declaration (continued) ,[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Arrays and the Instance Variable  length
Arrays and the Instance Variable  length  (continued) ,[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Arrays and the Instance Variable  length  (continued)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Processing One-Dimensional Arrays
Arrays (continued)  ,[object Object],[object Object],[object Object],[object Object],[object Object],double [] sales =  new   double [10]; int  index; double  largestSale, sum, average;
Code to Initialize Array to Specific Value (10.00) for  ( int   index = 0; index < sales.length; index++) sales[index] = 10.00;
Code to Read Data into Array for  ( int   index = 0; index < sales.length;  index++) sales[index] = console.nextDouble();
Code to Print Array for  ( int   index = 0; index < sales.length; index++) System.out.print(sales[index] + &quot; &quot;);
Code to Find Sum and Average of Array sum = 0; for  ( int   index = 0; index < sales.length;  index++) sum = sum + sales[index]; if  (sales.length != 0) average = sum / sales.length; else average = 0.0;
Determining Largest Element in Array maxIndex = 0;  for  ( int   index = 1; index < sales.length;  index++) if  (sales[maxIndex] < sales[index]) maxIndex = index; largestSale = sales[maxIndex];
Determining Largest Element in Array (continued)
Determining Largest Element in Array (continued)
Array Index Out of Bounds  ,[object Object],[object Object],[object Object],[object Object],[object Object]
Declaring Arrays as Formal Parameters to Methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Assignment Operators and Arrays
The Assignment Operators and Arrays (continued)
The Assignment Operators and Arrays (continued)
Relational Operators and Arrays ,[object Object],[object Object],[object Object],[object Object]
Relational Operators and Arrays (continued) boolean   areEqualArrays ( int [] firstArray,  int [] secondArray) { if  (firstArray.length != secondArray.length) return false ; for  ( int  index = 0; index < firstArray.length;  index++) if  (firstArray[index] != secondArray[index]) return false ;  return true ; } if  (areEqualArrays(listA, listB)) ...
Arrays as Parameter Methods
Methods for Array Processing
Methods for Array Processing (continued)
Methods for Array Processing (continued)
Methods for Array Processing (continued)
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Arrays of Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Array of  String  Objects ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Array of  String  Objects (continued)
Clock[] arrivalTimeEmp =  new  Clock[100];   Arrays of Objects (continued)
Instantiating Array Objects for  ( int  j = 0; j < arrivalTimeEmp.length; j++)  arrivalTimeEmp[j] =  new  Clock();
arrivalTimeEmp[49].setTime(8, 5, 10);   Instantiating Array Objects (continued)
Arrays and Variable Length Parameter List ,[object Object],[object Object]
Arrays and Variable Length Parameter List (continued)
Arrays and Variable Length Parameter List (continued)
Arrays and Variable Length Parameter List (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Arrays and Variable Length Parameter List (continued) ,[object Object],[object Object]
foreach  Loop  ,[object Object],[object Object],[object Object],[object Object]
foreach  loop (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Two-Dimensional Arrays
Two-Dimensional Arrays (continued)
double [][] sales =  new   double [10][5]; Two-Dimensional Arrays (continued)
[object Object],[object Object],[object Object],Accessing Array Elements
Accessing Array Elements (continued)
[object Object],[object Object],[object Object],[object Object],Two-Dimensional Arrays and the Instance Variable  length
Two-Dimensional Arrays and the Instance Variable  length  (continued)  ,[object Object],[object Object],[object Object],[object Object],[object Object]
Two-Dimensional Arrays: Special Cases
Two-Dimensional Arrays: Special Cases (continued) ,[object Object]
Two-Dimensional Array Initialization During Declaration
Two-Dimensional Array Initialization During Declaration (continued) ,[object Object],[object Object],[object Object]
Two-Dimensional Array Initialization During Declaration (continued)
Two-Dimensional Arrays (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Two-Dimensional Arrays: Processing Initialization for  ( int  row = 0; row < matrix.length; row++) for  ( int   col = 0; col < matrix[row].length; col++) matrix[row][col] = 10; Print for  ( int   row = 0; row < matrix.length; row++) { for  ( int   col = 0; col < matrix[row].length;  col++) System.out.printf(&quot;%7d&quot;, matrix[row][col]); System.out.println(); }
Two-Dimensional Arrays: Processing (continued) Input for  ( int   row = 0; row < matrix.length; row++) for  ( int   col = 0; col < matrix[row].length; col++) matrix[row][col] = console.nextInt();   Sum by Row for  ( int   row = 0; row < matrix.length; row++) { sum = 0; for  ( int   col = 0; col < matrix[row].length;  col++) sum = sum + matrix[row][col]; System.out.println(&quot;Sum of row &quot; + (row + 1)  + &quot; = &quot;+ sum); }
Two-Dimensional Arrays: Processing (continued) Sum by Column   for  ( int   col = 0; col < matrix[0].length; col++) { sum = 0; for  ( int   row = 0; row < matrix.length; row++) sum = sum + matrix[row][col]; System.out.println(&quot;Sum of column &quot; + (col + 1)  + &quot; = &quot; + sum); }
Two-Dimensional Arrays: Processing (continued) Largest Element in Each Row for  ( int   row = 0; row < matrix.length; row++) { largest = matrix[row][0];  for  ( int   col = 1; col < matrix[row].length;  col++) if  (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println(&quot;The largest element of row &quot;  + (row + 1) + &quot; = &quot; +  largest); }
Two-Dimensional Arrays: Processing (continued) Largest Element in Each Column for  ( int   col = 0; col < matrix[0].length; col++) { largest = matrix[0][col];  for  ( int   row = 1; row < matrix.length; row++) if  (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println(&quot;The largest element of col &quot;  + (col + 1) + &quot; = &quot; + largest); }
 
 
 
Multidimensional Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Loops to Process Multidimensional Arrays double [][][] carDealers =  new double [10][5][7]; for  ( int   i = 0; i < 10; i++) for  ( int   j = 0; j < 5; j++) for  ( int   k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;
Programming Example:  Text Processing ,[object Object],[object Object],[object Object]
Programming Example Solution:  Text Processing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter Summary (continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Arrays
ArraysArrays
Arrays
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chap09
Chap09Chap09
Chap09
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Array
ArrayArray
Array
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Array data structure
Array data structureArray data structure
Array data structure
 
1-D array
1-D array1-D array
1-D array
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Array
ArrayArray
Array
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 
List in Python
List in PythonList in Python
List in Python
 
2 arrays
2   arrays2   arrays
2 arrays
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 

Andere mochten auch

9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ
9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ
9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯMadpouzee Rubama
 
9591457 zh20130720 v
9591457 zh20130720 v9591457 zh20130720 v
9591457 zh20130720 vpricaramello
 
9789740331292
97897403312929789740331292
9789740331292CUPress
 
人生很多事情沒有答案 980405-教材-詹翔霖
人生很多事情沒有答案 980405-教材-詹翔霖人生很多事情沒有答案 980405-教材-詹翔霖
人生很多事情沒有答案 980405-教材-詹翔霖文化大學
 
ตารางสอนครุประสงค์
ตารางสอนครุประสงค์ตารางสอนครุประสงค์
ตารางสอนครุประสงค์Prasong Somarat
 
97 comunicado de_prensa_28032014
97 comunicado de_prensa_2803201497 comunicado de_prensa_28032014
97 comunicado de_prensa_28032014normasresumidas
 
9811000271 kashish sec 111 gurgaon resale manor one resale
9811000271 kashish sec 111 gurgaon resale manor one resale9811000271 kashish sec 111 gurgaon resale manor one resale
9811000271 kashish sec 111 gurgaon resale manor one resaleaddposting
 
99學年度暑期工作坊
99學年度暑期工作坊99學年度暑期工作坊
99學年度暑期工作坊llpseng
 
981202游泳池安全教育簡報
981202游泳池安全教育簡報981202游泳池安全教育簡報
981202游泳池安全教育簡報陳郁程
 
лаборатори 9
лаборатори 9лаборатори 9
лаборатори 9anjelo0028
 
99年度教育優先去計畫說明會指派業務承辦教導主任參加
99年度教育優先去計畫說明會指派業務承辦教導主任參加99年度教育優先去計畫說明會指派業務承辦教導主任參加
99年度教育優先去計畫說明會指派業務承辦教導主任參加Shi Guo Xian
 

Andere mochten auch (15)

9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ
9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ
9. ภาคบรรยายกลุ่ม 6 การวัด ประเมินผล ฯ
 
9789865966553 內文
9789865966553 內文9789865966553 內文
9789865966553 內文
 
97д
97д97д
97д
 
9591457 zh20130720 v
9591457 zh20130720 v9591457 zh20130720 v
9591457 zh20130720 v
 
9789740331292
97897403312929789740331292
9789740331292
 
95 752-82
95   752-8295   752-82
95 752-82
 
人生很多事情沒有答案 980405-教材-詹翔霖
人生很多事情沒有答案 980405-教材-詹翔霖人生很多事情沒有答案 980405-教材-詹翔霖
人生很多事情沒有答案 980405-教材-詹翔霖
 
97s
97s97s
97s
 
ตารางสอนครุประสงค์
ตารางสอนครุประสงค์ตารางสอนครุประสงค์
ตารางสอนครุประสงค์
 
97 comunicado de_prensa_28032014
97 comunicado de_prensa_2803201497 comunicado de_prensa_28032014
97 comunicado de_prensa_28032014
 
9811000271 kashish sec 111 gurgaon resale manor one resale
9811000271 kashish sec 111 gurgaon resale manor one resale9811000271 kashish sec 111 gurgaon resale manor one resale
9811000271 kashish sec 111 gurgaon resale manor one resale
 
99學年度暑期工作坊
99學年度暑期工作坊99學年度暑期工作坊
99學年度暑期工作坊
 
981202游泳池安全教育簡報
981202游泳池安全教育簡報981202游泳池安全教育簡報
981202游泳池安全教育簡報
 
лаборатори 9
лаборатори 9лаборатори 9
лаборатори 9
 
99年度教育優先去計畫說明會指派業務承辦教導主任參加
99年度教育優先去計畫說明會指派業務承辦教導主任參加99年度教育優先去計畫說明會指派業務承辦教導主任參加
99年度教育優先去計畫說明會指派業務承辦教導主任參加
 

Ähnlich wie 9781439035665 ppt ch09

Ähnlich wie 9781439035665 ppt ch09 (20)

07 Arrays
07 Arrays07 Arrays
07 Arrays
 
Chap10
Chap10Chap10
Chap10
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
 
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
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Learn Java Part 8
Learn Java Part 8Learn Java Part 8
Learn Java Part 8
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
Arrays
ArraysArrays
Arrays
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
Arrays
ArraysArrays
Arrays
 
Python list
Python listPython list
Python list
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mehr von Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

9781439035665 ppt ch09