170 likes | 284 Views
Structuring Data in a Linear Array. Learning Objectives. When processing lots of data We learn how to store data in linear structures We learn how to Create these linear structures called Arrays Initialize the elements in the structure Manipulate the elements in the array
E N D
Learning Objectives • When processing lots of data • We learn how to store data in linear structures • We learn how to • Create these linear structures called Arrays • Initialize the elements in the structure • Manipulate the elements in the array • Read into and write from the structure • And more……
Suppose you have to remember 10 things • So you write • String first = new String(“me1”); • String second = new String(“me2”); • String third = new String(“me3”); • String fourth = new String(“me4”); • String fifth = new String(“me5”); • String sixth = new String(“me6”); • String seventh = new String(“me7”); • ….
There is a lot of typing is there a better way to store these variables?Yes
Most Programming Languages provide ways to store many things in a contiguous listlike ….
An array Characteristics - A contiguous block of memory spaces - Each one is referred by an index - indices start from 0 and go to n-1 - a “homogeneous” structure
Defining an array int[ ] A; A= new int[10]; /* initialize elements */ A[0] = 10; A[1] = 20; …..
Define an array of any type int[ ] A = new int[10]; String[ ] A = new String[100]; double[ ] A = new double[8858]; boolean[ ] A = new boolean[n]; ….
Length of an array A is given byA.lengthlength is an attribute of A So anytime we need the length it is available
An Application Qatar population from 2000-2007 Store in an array What can I do with this?
Pretty much anything… • Find the year of the maximum population • Find the average population per year 2000-2007 • Find the year of the minimum population • Find the median population between 2000-2007 • Find the years where the population increased the most We will look at these questions in Classwork
More examples of Arrays Slide courtesy of Tom Cortina
Examples /* Assume highTemp is an array of ints Slide courtesy of Tom Cortina
Be careful with Array index out of bounds errors Slide courtesy of Tom Cortina