Introduction to arrays Array One dimenstional array.

28
Introduction to arrays Array One dimenstional array

Transcript of Introduction to arrays Array One dimenstional array.

Page 1: Introduction to arrays Array One dimenstional array.

Introduction to arrays Array

One dimenstional array

Page 2: 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

Page 3: Introduction to arrays Array One dimenstional array.

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

Page 4: Introduction to arrays Array One dimenstional array.

Array index

marks

index

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

Page 5: Introduction to arrays Array One dimenstional array.

Array indexwith N values

Nmarks

index

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

N-1

Page 6: Introduction to arrays Array One dimenstional array.

How to reference a value in an array?

Nmarks

index

A 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]

99

N-1

Page 7: Introduction to arrays Array One dimenstional array.

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 arrayname followed by the index in brackets

Page 8: Introduction to arrays Array One dimenstional array.

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];

Page 9: Introduction to arrays Array One dimenstional array.

Array declaration (cont)• We can also declare and assign a memory space at the same time

– int [] a = new int[10];

Page 10: Introduction to arrays Array One dimenstional array.

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} ;

10 5 22 11 13 15 18 8 266

Page 11: Introduction to arrays Array One dimenstional array.

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];

Page 12: Introduction to arrays Array One dimenstional array.

Array declarationInitializer 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”};

Page 13: Introduction to arrays Array One dimenstional array.

Array declarationInitializer 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

Page 14: Introduction to arrays Array One dimenstional 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.

Page 15: Introduction to arrays Array One dimenstional array.

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 student’s ID number and then the averagefor that assignment. Write a program, MarksArray.java to achieve this?

Page 16: Introduction to arrays Array One dimenstional array.

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 student’s 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.

Page 17: Introduction to arrays Array One dimenstional array.

Array length

• Int [] ar = new int [10];

• a.length = 10

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

Page 18: Introduction to arrays Array One dimenstional array.

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 ();

}}

Page 19: Introduction to arrays Array One dimenstional array.

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

Page 20: Introduction to arrays Array One dimenstional array.

Example

• Int [] ar = new int [10];• Ar[10]=86;• It prints an error• Exception in thread “main”

java.lang.ArrayIndexOutOfBoundException:86

Page 21: Introduction to arrays Array One dimenstional array.

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] ); }}

Page 22: Introduction to arrays Array One dimenstional array.

Example• Write a java program that stores the first hundred positive integers in an

array and prints them in a reverse order.

Page 23: Introduction to arrays Array One dimenstional array.

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.

Page 24: Introduction to arrays Array One dimenstional array.

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.

Page 25: Introduction to arrays Array One dimenstional 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 id Mark– 12009 15

Page 26: Introduction to arrays Array One dimenstional array.

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.

Page 27: Introduction to arrays Array One dimenstional array.

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.

Page 28: Introduction to arrays Array One dimenstional array.

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’.