Introduction to arrays Array

  • Upload
    gladys

  • View
    58

  • Download
    3

Embed Size (px)

DESCRIPTION

Introduction to arrays Array. One dimenstional array. outlines. What is an array Why arrays How to declare and initialise an array How to use for loops to process arrays Array index out of bounds exceptions. Array definition. - PowerPoint PPT Presentation

Citation preview

  • Introduction to arrays Array One dimenstional array

  • outlinesWhat is an arrayWhy arraysHow to declare and initialise an arrayHow to use for loops to process arraysArray index out of bounds exceptions

  • Array definitionAn array is the memory of the computer used to store lots of data items of the same types.

    An array is an ordered list of value

  • Array indexmarksindexThe array marks holds 10 marks:The 1st mark is indexed by 0 The last mark is indexed by 9(10-1)

  • Array indexwith N valuesNmarksindexThe array Nmarks array holds N marks:The 1st mark is indexed by 0 The last mark is indexed by (N-1)N-1

  • How to reference a value in an array?NmarksindexA particular value in an array is referenced using the arrayname followed by the index in bracketsSystem.out.println(Nmarks[0]); will print 79, Nmarks[9] 99N-1

  • How to reference a value in an array?marksindexmarks[0] refers to 79 where as marks[9] refers to 91A particular value in an array is referenced using the arrayname followed by the index in brackets

  • Array declarationWe first declare a as an array of integers. int [] a ; We the give it a space in the memory to hold 10 items(integes).a new in[10];

  • Array declaration (cont)We can also declare and assign a memory space at the same timeint [] a = new int[10];

  • Array declaration and initialisationWe can also declare and assign a memory space at the same timeint [] a = {10, 5, 6, 22, 11, 13, 15, 81, 8,26} ;

    10522111315188266

  • Array declaration(Cont)An array can hold only objects of the same type specified in the declaration step.Int [] intarr = new int[5];String [] starr= new String[5];Char [] charr= new Char[5]; Doucle [] darr= new double[5];Boolean [] barr= new boolean[5];

  • Array declarationInitializer Lists (cont)An initializer list can be used to instantiate and initialize an array in one stepThe values are delimited by braces and separated by commasExamples:int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476};char[] letterGrades = {'A', 'B', 'C', 'D', 'F'};String [] names= {xxxx, yyy, xx, tt,dddddddd};

  • Array declarationInitializer Lists (cont)Note that when an initializer list is used:the new operator is not usedno size value is specifiedThe size of the array is determined by the number of items in the initializer listAn initializer list can only be used in the declaration of an array

  • Array declaration (cont)int[] x, y, z;identifies that x, y and z are all arrays.

    int x[], y, z;identifies that only x is an array, y and z are simply integers.

  • Why arrays?Imagine you want to write a program that asks the user to enterin 50 student midterm marks. The program would then displayeach grade with the students ID number and then the averagefor that assignment. Write a program, MarksArray.java to achieve this?

  • Why arrays? (cont)Imagine you want to write a program that asks the user to enterin 50 student midterm marks. The program would then displayeach grade with the students ID number and then the averagefor that assignment in a nice report.

    How would you do it?One way would be to provide 100 variables:int mark1, mark2, mark3, (until 50)int ID1, ID2, ID3, (until 50)The array provides for a way to create only two variables:int marks[] = new int[50];int IdNumbers[] = new int[50];This much easier then declaring 100 variables.

  • Array lengthInt [] ar = new int [10];

    a.length = 10

    The values are a[0] to a[a.length-1]

  • Primes.javapublic class Primes{public static void main (String[] args){int[] primes = {2, 3, 5, 7, 11, 13, 17, 19,23};System.out.println ("Array length: " + primes.length);System.out.println ("The first few prime numbers are:");for (int prime = 0; prime < primes.length; prime++)System.out.print (primes[prime] + " ");System.out.println ();}}

  • Array index out of boundsOnce an array is created, it has a fixed sizeAn index used in an array reference must specify a valid elementThat is, the index value must be in bounds (0 to N-1)The Java interpreter will throw an exception if an array index is out of boundsThis is called automatic bounds checking

  • ExampleInt [] ar = new int [10];Ar[10]=86;It prints an errorException in thread main java.lang.ArrayIndexOutOfBoundException:86

  • Example// StudentArray.java: store integers in arrays and accesspublic class StudentArray{ public static void main(String[ ] args) { int[] students = {55, 69, 70, 30, 80}; System.out.println("Array Length = " + students.length); System.out.println("Values Stored in Array:"); for ( int i=0; i < students.length; i+ +) System.out.println(students[ i] ); }}

  • ExampleWrite a java program that stores the first hundred positive integers in an array and prints them in a reverse order.

  • ExampleWrite a java program that stores that allows the user to enter 10 positive integers, stores them in an array. Then prints how many even number were enter if any.

  • ExampleWrite a program that allows the user to enter 10 integers stores them in an array and search for largest and the smallest in the array.

  • ExampleWrite a java program that uses two array of length 10: arrID stores the students id and arrMark stores the students mark. For each students the program should print the student and id and its mark as follows:Student idMark1200915

  • Example

    Write a numbera program that generates 100 random integers between 0 to 99 and stores them any array and prints the largest, smallest and the average.

  • Example

    Write a program where the user types in a number of Strings which are stored in an array of Strings and then the program prints out all the longest Strings entered by the user.

  • Example

    Write a program where the user types in a number of Strings stored in an array of Strings and then the program prints out the string that has the most occurrences of the character a.

    **********************