1 / 17

Structuring Data in a Linear Array

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

odele
Download Presentation

Structuring Data in a Linear Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structuring Data in a Linear Array

  2. 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……

  3. 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”); • ….

  4. There is a lot of typing is there a better way to store these variables?Yes

  5. Most Programming Languages provide ways to store many things in a contiguous listlike ….

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

  7. Defining an array int[ ] A; A= new int[10]; /* initialize elements */ A[0] = 10; A[1] = 20; …..

  8. Can you have an array of any type?

  9. 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]; ….

  10. Length of an array A is given byA.lengthlength is an attribute of A So anytime we need the length it is available

  11. How can I use an array?

  12. An Application Qatar population from 2000-2007 Store in an array What can I do with this?

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

  14. More examples of Arrays Slide courtesy of Tom Cortina

  15. Examples /* Assume highTemp is an array of ints Slide courtesy of Tom Cortina

  16. Be careful with Array index out of bounds errors Slide courtesy of Tom Cortina

  17. Do Some examples in Class

More Related