Introduction to arrays Array One dimenstional array.

Presentation on theme: "Introduction to arrays Array One dimenstional array."— Presentation transcript:

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 An 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 index marks index The array marks holds 10 marks: The 1 st mark is indexed by 0 The last mark is indexed by 9(10-1)

Array index with N values Nmarks index The array Nmarks array holds N marks: The 1 st mark is indexed by 0 The last mark is indexed by (N-1) N-1

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

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

Array declaration We 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 time – int [] a = new int[10];

Array declaration and initialisation We can also declare and assign a memory space at the same time – int [] 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 declaration Initializer Lists (cont) An initializer list can be used to instantiate and initialize an array in one step The values are delimited by braces and separated by commas Examples: – 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 declaration Initializer Lists (cont) Note that when an initializer list is used: – the new operator is not used – no size value is specified The size of the array is determined by the number of items in the initializer list An 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 enter in 50 student midterm marks. The program would then display each grade with the students ID number and then the average for 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 enter in 50 student midterm marks. The program would then display each grade with the students ID number and then the average for 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 length Int [] ar = new int [10]; a.length = 10 The values are a[0] to a[a.length-1]

Primes.java public 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 bounds Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That 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 bounds This is called automatic bounds checking

Example Int [] ar = new int [10]; Ar[10]=86; It prints an error Exception in thread main java.lang.ArrayIndexOutOfBoundException:86

Example // StudentArray.java: store integers in arrays and access public 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] ); }

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

Example Write 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.

Example Write 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.

Example Write 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 idMark – 1200915

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.