Your SlideShare is downloading. ×
0
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
Java: Introduction to Arrays
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

Java: Introduction to Arrays

13,802

Published on

Sub: Java …

Sub: Java
Topic: Introduction to Arrays
Slide number: 5
Presented by: Mahbubul Islam (MMI)
Lecturer, Dept. of CSE
University of Rajshahi

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

No Downloads
Views
Total Views
13,802
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
226
Comments
0
Likes
1
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. Introduction to Arrays
  • 2. What are 1-D arrays? <ul><li>  </li></ul><ul><li>An array is an object that is used to store a list of values of the same type . </li></ul><ul><li>It is made out of a contiguous block of memory that is divided into a number of &quot;slots“ or variables. </li></ul><ul><li>Each slot can be accessed by using its index (or subscript) . </li></ul><ul><li>If there are N slots in an array, the indexes will be 0 through N-1. </li></ul><ul><li>The diagram on the right shows a 5-element array. The indexes are from 0 to 4 </li></ul><ul><li>An array that uses a single subscript is called a one dimensional array. </li></ul><ul><li>  </li></ul><ul><li>Two dimensional arrays, three dimensional arrays, and higher dimensional arrays also </li></ul><ul><li>exist: </li></ul>
  • 3. 1-D Array Declaration <ul><li>For any type T, T[ ] is a class , whose instances are arrays of type T. </li></ul><ul><li>Thus, the following statement declares a reference variable, b, of type T array : </li></ul><ul><li>T[] b; </li></ul><ul><li>For any positive integer n, the following expression creates a new T[ ] object of size n and stores its reference in b : </li></ul><ul><li>b = new T[n] ; </li></ul><ul><li>As usual, the two expressions can be combined together as: </li></ul><ul><li>T[] b = new T[n] ; </li></ul><ul><li>  </li></ul><ul><li>For example, the following declares an int[] , grades, of size 10: </li></ul><ul><li>  int[ ] grades = new int[10]; </li></ul>
  • 4. 1-D Array Declaration (cont’d) <ul><li>The declaration of an array of size n creates n variables of the base type T. </li></ul><ul><li>These variables are indexed starting from 0 to n-1. They are called subscripted variables . </li></ul><ul><ul><li>A subscripted variable can be used anywhere an ordinary variable of the same type can be used. </li></ul></ul><ul><li>The slots of an array are initialized to the default value for their type. </li></ul><ul><ul><li>Each slot of a numeric array is initialized to zero. </li></ul></ul><ul><li>Each array object has a public instance variable, length, that stores the size of the array. </li></ul><ul><ul><li>Thus, the following statement prints 10, the size of grades: </li></ul></ul><ul><ul><li>System.out.println(grades.length); </li></ul></ul><ul><ul><li>Once an array has been constructed, its length does not change. </li></ul></ul><ul><li>Other examples of 1D-array declaration are: </li></ul><ul><li>double[ ] price = new double[500]; // each element is initialized to 0.0 </li></ul><ul><li>boolean[ ] flag = new boolean[20]; // each element is initialized to false </li></ul>
  • 5. Accessing elements of a 1-D Array <ul><li>A particular variable is accessed by indexing the array reference with the index (subscript) of the variable in square brackets : </li></ul><ul><li>grades[4] = 20; </li></ul><ul><li>The following example, re-initializes each variable with twice its index: </li></ul><ul><ul><li>int[] grades = new int[10]; </li></ul></ul><ul><ul><li> for(int i = 0; i < grades.length; i++) </li></ul></ul><ul><ul><li>grades[i] = 2*i ; </li></ul></ul><ul><li>The use of grades.length instead of 10 makes the code more general. </li></ul>
  • 6. Accessing elements of a 1-D Array (cont'd ) <ul><li>The following prints the values of the array re-initialized by the example in the previous slide. </li></ul><ul><li>  </li></ul><ul><li>for(int i = 0; i < grades . length; i++) </li></ul><ul><li>System.out.print(grades[i] + “ “); </li></ul><ul><li>Output: </li></ul>0 2 4 6 8 10 14 16 18 <ul><li>Note: Trying to access an element with an invalid index causes a run-time error: </li></ul><ul><li>ArrayIndexOutOfBoundsException: </li></ul><ul><li>int x = grades[10]; // causes run-time error </li></ul>
  • 7. Accessing elements of a 1-D Array (cont'd ) <ul><li>The indexes (subscripts) of an array must always be of integer type except long </li></ul><ul><li>( int, char, short, byte ). </li></ul><ul><li>An array index can be any expression that evaluates to an integer within the allowed index range: </li></ul><ul><li>double[] val = new double[20]; int k = 3; </li></ul><ul><li>val[0] = 1.5; </li></ul><ul><li>val[1] = 10.0; </li></ul><ul><li>val[2] = 15.5; </li></ul><ul><li>val[k] = val[k-1] + val[k - 2]; // same as val[3] = val[2] + val[1] </li></ul><ul><li>val[ k*2 + 3 ] = 24 + val[13]; // same as: val[ 9 ] = 24 + 0.0 </li></ul><ul><li>System.out.println( &quot;val[&quot; + k + &quot;] == &quot; + val[k] ); // output: val[3] = 25.5 </li></ul><ul><li>Note: the following are invalid because the index is not of type int, char, short, or byte: </li></ul><ul><li>long k = 5L; </li></ul><ul><li>val[k] = 33.2; </li></ul><ul><li>val[8.0] = 75.5;  </li></ul>
  • 8. Using Subscripts with an Array <ul><li>Enhanced for loop </li></ul><ul><ul><li>Cycle through array </li></ul></ul><ul><ul><li>Without specifying starting and ending points for loop control variable </li></ul></ul><ul><ul><li>for(int val : scoreArray) </li></ul></ul><ul><ul><ul><li>System.out.println(val); </li></ul></ul></ul>
  • 9. Initializer List <ul><li>Initializer list can be used to instantiate and initialize an array in one step: </li></ul><ul><li>int[ ] prime = {2 , 3, 5, 7, 11, 13, 17, 19, 23, 29} ; </li></ul><ul><li>char[ ] letterGrade = { ’ A ’ , ‘ B ’ , ‘C’, ‘ D ’ , ‘ F ’ }; </li></ul><ul><li>It is actually the compiler that fills the gap. Thus, in the first example, the compiler would </li></ul><ul><li>add the following: </li></ul><ul><li>int[] prime = new int[10]; </li></ul><ul><li>prime[0] = 2; prime[1] = 3; ... prime[9] = 29; </li></ul><ul><li>Observe that when an initializer list is used: </li></ul><ul><ul><li>The new operator is not required. </li></ul></ul><ul><ul><li>The size is not required; it is computed by the compiler. </li></ul></ul>
  • 10. Copying one array to another <ul><li>Consider the following declarations: </li></ul><ul><li>int[ ] array1 = {22, 3, 50, 7, 11, 13, 17}; </li></ul><ul><li>int[ ] array2 = {3, 5, 8, 20, 0, 100, 40}; </li></ul><ul><li>The assignment statement : </li></ul><ul><li>array1 = array2; </li></ul><ul><li>does not copy the contents of array2 to array1 ; it makes the reference array1 to refer to the array object referenced by array2 . The object that was referenced by array1 becomes garbage. </li></ul><ul><li>To copy the contents of array2 to array1 , code like the following is used: </li></ul><ul><ul><li>for(int k = 0; k < array2.length; k++) </li></ul></ul><ul><ul><li>array1[k] = array2[k]; </li></ul></ul>
  • 11. Array used as a Parameter <ul><li>Remember that a parameter is data that is supplied to a method just before it starts running. </li></ul><ul><li>The following method prints the content of an int array passed to it as parameter: </li></ul><ul><ul><li>class MyArray { </li></ul></ul><ul><ul><li>public void print (int[] x) { </li></ul></ul><ul><ul><li> for(int i = 0; i < x.length; i++) </li></ul></ul><ul><ul><li> System.out.print(x[i]+ &quot; &quot;); </li></ul></ul><ul><ul><li> System.out.println(); </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>The method is written using the parameter x to refer to the actual data that it will be supplied with. </li></ul><ul><li>The method is written without referring to any particular data. When the print() method is running it has a reference to an array object in its parameter x . </li></ul>
  • 12. Example(1) <ul><li>class ArrayDemo { </li></ul><ul><li>public static void main ( String[] args ) { </li></ul><ul><li>MyArray operate = new MyArray(); </li></ul><ul><li>int[] ar1 = { -20, 19, 1, 5, -1, 27 } ; </li></ul><ul><li>System.out.print (&quot;The array is: &quot; ); </li></ul><ul><li>operate.print( ar1 ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>public static void print (int[] x) { for(int i = 0; i < x.length; i++) System.out.print(a[x]+ &quot; &quot;); System.out.println(); } MyArray print(x) operate -20 19 1 5 -1 27 ar1
  • 13. Parameter Connected to New Data <ul><li>Could the main() method create a second array and use the print() method with it? </li></ul><ul><li>Example (2): What is the output? </li></ul><ul><li>class ArrayDemo { </li></ul><ul><li>public static void main ( String[] args ) { </li></ul><ul><li>MyArray operate = new MyArray(); </li></ul><ul><li>int[] ar1 = { -20, 19, 1, 5, -1, 27 } ; </li></ul><ul><li> int[] ar2 = {1, 2, 6, 3, 9, 5}; </li></ul><ul><li>System.out.print(&quot;The array is: &quot; ); </li></ul><ul><li>operate.print( ar1 ); </li></ul><ul><li> System.out.print(&quot;The second array is: &quot; ); </li></ul><ul><li> operate.print( ar2 ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>The array is: -20 19 1 5 -1 27 The second array is: 1 2 6 3 9 5
  • 14. Using Two-Dimensional and Multidimensional Arrays <ul><li>One-dimensional or single-dimensional array </li></ul><ul><ul><li>Picture as column of values </li></ul></ul><ul><ul><li>Elements accessed using single subscript </li></ul></ul><ul><li>Two-dimensional arrays </li></ul><ul><ul><li>Two or more columns of values </li></ul></ul><ul><ul><li>Rows and columns </li></ul></ul><ul><ul><li>Use two subscripts </li></ul></ul><ul><ul><li>Matrix or table </li></ul></ul><ul><ul><li>int[][] someNumbers = new int[3][4]; </li></ul></ul>
  • 15. View of a Two-Dimensional Array in Memory
  • 16. Using Two-Dimensional and Multidimensional Arrays (continued) <ul><li>int[][] rents = { {400, 450, 510}, </li></ul><ul><ul><li>{500, 560, 630}, </li></ul></ul><ul><ul><li>{625, 676, 740}, </li></ul></ul><ul><ul><li>{1000, 1250, 1600} }; </li></ul></ul><ul><li>public static void displayScores(int[][]scoresArray) </li></ul>
  • 17. Using Two-Dimensional and Multidimensional Arrays (continued) <ul><li>Multidimensional arrays </li></ul><ul><ul><li>More than two dimensions </li></ul></ul><ul><li>Create arrays of any size </li></ul><ul><ul><li>Keep track of order of variables needed as subscripts </li></ul></ul><ul><ul><li>Don’t exhaust computer’s memory </li></ul></ul>

×