Arrays Class presentation
-
Upload
neveen-reda -
Category
Education
-
view
654 -
download
0
description
Transcript of Arrays Class presentation
![Page 1: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/1.jpg)
Introduction to Computer ProgrammingLab 2
Arrays
Neveen Reda
![Page 2: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/2.jpg)
Arrays in action0There are many uses for arrays0For example image pixels are represented using
arrays
Neuroph (2012)
![Page 3: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/3.jpg)
Arrays in action
0Arrays can also be used in creating grids for games.
Eckert (2009)
![Page 4: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/4.jpg)
Lesson Objectives
After this lesson you will be able to:
1.Develop Java programs that create arrays and perform different operations on them.
2.Determine the functionality and output of any given program that performs array manipulations.
![Page 5: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/5.jpg)
Arrays0 Why arrays?
Individual variables can only hold one value at a time, Arrays can holds multiple values (Abdennadher , 2010).
0 “An array is a data structure that defines an indexed collection of a fixed number of data elements.” (Mughal & Rasmussen, 2009).
![Page 6: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/6.jpg)
Arrays0 The position of an element in an array is called the index.0 If array X contains 10 elements, the first element will be at
position (index) '0' and the last element is at position '9'
Oracle (2012)
![Page 7: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/7.jpg)
Arrays Declarations0 All the elements in an array have the same data type.
(For example all integers, all Strings, all doubles and so on)
Examples:
int [] x; //x is an array of integers.
double [] myArray; //myArray is an array of doubles.
![Page 8: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/8.jpg)
Creating Arrays0 The size if an array is fixed. It is specified when the array is
created and cannot be changed (Abdennadher , 2010).
x = new int[5]; //array x is now of size 5
myArray = new double[3];
![Page 9: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/9.jpg)
Creating Arrays0 An array can be declared and created in one statement.0 Until this point the arrays is empty (contains default values
of its declared type)
Examples:
int [] x = new int [5];
double [] myArray = new double [3];
int number = 10;
char [] letters = new char[number];
![Page 10: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/10.jpg)
Initializing Arrays
Double [] prices = new double [5];
![Page 11: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/11.jpg)
Initializing Arrays
Double [] prices = {2.25, 3.0, 10.5, 5.3, 0.5};
2.25
3.0
10.5
5.3
0.5
![Page 12: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/12.jpg)
Using ArraysAfter creating and initializing an array, it can be used
within a program:
1. An element in the array can be used within a statement
2. The reference to the array can be used within a statement. (Abdennadher , 2010)
![Page 13: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/13.jpg)
Using Arrays0 Reassign value to index:
0 Using element within a statement:
3.0
10.5
5.3
0.5
Prices[0] = 4.5;
if(Prices[1] >= 3.0){
Discount = 10;
}
2.254.5
![Page 14: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/14.jpg)
Array Examples
0 Write a java program that creates an array of size 3 and prints all its elements using a for loop.
![Page 15: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/15.jpg)
![Page 16: Arrays Class presentation](https://reader033.fdocuments.net/reader033/viewer/2022061212/54956b36ac7959342e8b4e4c/html5/thumbnails/16.jpg)
References• Abdennadher S. (2010). Arrays. Lecture notes.
• Eckert T. (2009). Bimaru - Battleship Solitaire [image]. Retrieved from: http://www.cyrket.com/p/android/com.androidcan.bimaru/
• Mughal K. A. & Rasmussen R. W. (2009). A Programmer's Guide to Java SCJP Certification. Upper Saddle River, NJ: Pearson Education.
• Neuroph (2012). Image colors [image]. Retrieved from: http://neuroph.sourceforge.net/image_recognition.html
• Oracle (2012). An array of 10 elements [image]. Retrieved from: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html