Arrays Class presentation

1,906 views

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,906
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
161
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Arrays Class presentation

  1. 1. Introduction to Computer Programming Lab 2 Arrays Neveen Reda
  2. 2. Arrays in action0 There are many uses for arrays0 For example image pixels are represented using arrays Neuroph (2012)
  3. 3. Arrays in action 0 Arrays can also be used in creating grids for games.Eckert (2009)
  4. 4. Lesson ObjectivesAfter this lesson you will be able to:3.Develop Java programs that create arrays and performdifferent operations on them.5.Determine the functionality and output of any givenprogram that performs array manipulations.
  5. 5. 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).
  6. 6. 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)
  7. 7. 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.
  8. 8. 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 5myArray = new double[3];
  9. 9. 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];
  10. 10. Initializing ArraysDouble [] prices = new double [5];
  11. 11. Initializing ArraysDouble [] prices = {2.25, 3.0, 10.5, 5.3, 0.5}; 2.25 3.0 10.5 5.3 0.5
  12. 12. Using ArraysAfter creating and initializing an array, it can be used within a program:• An element in the array can be used within a statement• The reference to the array can be used within a statement. (Abdennadher , 2010)
  13. 13. Using Arrays0 Reassign value to index: 4.5 2.25 Prices[0] = 4.5; 3.0 10.5 5.3 0.50 Using element within a statement:if(Prices[1] >= 3.0){Discount = 10;}
  14. 14. Array Examples0 Write a java program that creates an array of size 3 and prints all its elements using a for loop.
  15. 15. 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 Programmers 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

×