Your SlideShare is downloading. ×
Array lecture
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Array lecture

210
views

Published on

Published in: Education, Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
210
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. At the end of the lesson, the student should be able to:  Declare and create arrays  Access array elements  Determine the number of elements in an array  Declare and create multidimensional arrays  Describe how to use arrays to manage multiple values in the same variable.
  • 2.   Suppose we have here three variables of type int with different identifiers for each variable. int number1; int number2; int number3; number1 = 1; number2 = 2; number3 = 3; As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.
  • 3. In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array. ● An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots. ●
  • 4.  The Java programming language allows you to group multiple values of the same type (lists) using one-dimensional arrays. Arrays are useful when you have related pieces of data (such as the ages for several people),but you do not want to create separate variables to hold each piece of data
  • 5. Array of int 0 Number: 1 2 0 1 2 Array of flowers
  • 6.  To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name. Syntax: type [] array_identifier; Where:  The type represents the primitive data type or object type for the values stored in the array.  The [] informs the compiler that you are declaring an array  The array_identifier is the name that you are assigning to refer to the array. For example, int []ages; or int ages[];
  • 7. After declaring, we must create the array and specify its length with a constructor statement. Syntax: array_identifier = new type [length]; where:  The array_identifier is the name you are assigning to reference the array.  The type represents the primitive data type or object type for the value stored in the array.  The length represent the size of the array. 
  • 8.  Use the following code to instantiate an array of char called status and an array of int called ages. status = new char [20]; ages = new int [5];
  • 9.  You can fill the contents of an array after you have created the array. The syntax for setting the values in an array is: array_identifier[index] = value; where:  The array_identifier is the name you are   assigning to the array. The index represents the location in the array where the value will be placed. The value is the value you are assigning to index in the array.
  • 10. ages[0] ages[1] ages[2] ages[3] ages[4] ages: 0 19 = = = = = 19; 42; 92; 33; 46; 1 42 2 92 3 33 4 46
  • 11. If you know the values you want in your array at the time that you declare the array, you can declare, instantiate, and set the values for an Array object in the same line of code. The syntax for this combined declaration, instantiation, initialization of values is:  type [] array_identifier = {comma-separated_list_of_values_or_expressions}; where: ● The type represents the primitive data type or object type for the values stored in the array. ● The [] informs the compiler that you are declaring an array. ● The array_identifier is the name you are assigning to the array. ● The {comma-separated_list_of_values_or_expressions} represents a list of values you want to store in the array or a list of expressions with results that will be stored in the array.
  • 12. type [] array_identifier = {commaseparated_list_of_values_or_expressions}; where:  The type represents the primitive data type or object type for the values stored in the array.  The [] informs the compiler that you are declaring an array.  The array_identifier is the name you are assigning to the array.   The {commaseparated_list_of_values_or_expressions} represents a list of values you want to store in the array or a list of expressions with results that will be stored in the array.
  • 13. The following statement combines the previous declaration, instantiation, and initialization examples for the ages array  int [] ages = {19, 42, 92, 33, 46};
  • 14. //creates an array of boolean variables with identifier //results. This array contains 4 elements that are //initialized to values {true, false, true, false} boolean results[] = { true, false, true, false }; //creates an array of 4 double variables initialized //to the values {100, 90, 80, 75}; double []grades = {100, 90, 80, 75}; //creates an array of Strings with identifier days and //initialized. This array contains 7 elements String days[]= {“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”,“Sun”};
  • 15. Each element of an array is accessed using its index. To access a value from the array, state the array name and the index number for the element (in braces []) on the right side of an assignment operator. The following code example demonstrates how to set the value at a particular index in an array: status[1] = 3 names[2] = “Johann"; ages[2] = 10; prices[3] = 9.99F;
  • 16. The following code example demonstrates how to retrieve values from a particular index in an array: char s = status[1]; String name = names [2]; int age = ages[2]; double price = prices[3]; 