Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 01234567891011 The index of the first position in an.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would.
Chubaka Producciones Presenta :.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 10 Arrays and Collections.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
Chicas, este calendario si es pa' nosotras !!!!!.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Array Basics An array is a collection of data.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Arrays and Pointers (part 2) CSE 2031 Fall March 2016.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Java Programming Language Lecture27- An Introduction.
CS1101: Programming Methodology
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 10 Arrays Animated Version
Java Review: Reference Types
Advanced Programming Behnam Hatami Fall 2017.
Basic Files + (Chapter 10 – Arrays) : Objectives
Chapter 6: Arrays.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6: Arrays.
McDonald’s Kalender 2009.
McDonald’s calendar 2007.
Java Programming Language
Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can,
February 2007 Note: Source:.
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
SPL – PS2 C++ Memory Handling.
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall The index of the first position in an array is 0.

Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] rainfall = new double[12]; rainfall rainfall[2] This indexed expression refers to the element at position #2

Array Lengths double[] rainfall = new double[12]; double annualAverage; double sum = 0.0; int index; for (index = 0; index < rainfall.length; index++) { rainfall[index] = SavitchIn.readLineDouble(); sum += rainfall[index]; } annualAverage = sum / rainfall.length; The public constant length returns the capacity of an array.

Example Programs Phrrud … out to reality … ArrayRain.java Phrrud … out to reality … ArrayAverages.java

Array Bounds Errors Trying to access an array element that does not exist causes a runtime error Negative indices Indices beyond the size Falop … out to reality … ArrayBoundsError.java

Array Initialization Like other data types, it is possible to declare and initialize an array at the same time. int[] number = { 2, 4, 6, 8 }; double[] samplingData = { 2.443, 8.99, 12.3, , 18.2, 9.00, 3.123, , }; String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; The capacity of the array is set to the number of elements in the list. number.length samplingData.length monthName.length

Example Programs Groeeet … out to reality … ArrayInit.java An array variable can be explicitly made to point to no data, using the null value Spaaocie … out to reality … ArrayNULL.java

References are Pointers A reference variable points to an object So, arrays are objects, but don't worry about that now But it does mean you can Have multiple references to an array Not copy an array with = Lose an array

Having Two References to an Array Code State of Memory A A int[] clemens, twain; B B clemens = new int[5]; C C twain = clemens; A. A. Variables are allocated in memory. clemens twain B. clemens B. The reference to the new array is assigned to clemens. Array in Memory Array in Memory C. clemens customer. C. The reference in clemens is assigned to customer.

Example Program Dooop … out to reality … ArrayDup.java

Cloning an Array An array can be copied using the clone method It's necessary to cast the clone to the right array type Babababoom … out to reality … ArrayClone.java

Losing an Array Code State of Memory customer A. A. The variable is allocated in memory. A A int[] customer; B B customer = new int[5]; C C B. customer B. The reference to the new array is assigned to customer. Array in Memory Array in Memory C. customer. C. The reference to another array overwrites the reference in customer. Array in Memory Array in Memory

Garbage Collection An array that has no references is garbage collected by the java program Spaaocie … out to reality … ArrayGC.java

Two-Dimensional Arrays Two-dimensional arrays are useful in representing tabular information.

Example Programs Ieeei … out to reality … ArrayMatrix.java Ieeei … out to reality … ArrayCalendar.java Ieeei … out to reality … ArrayCube.java

Multi-dimensional Arrays … NOT Java does not really have multi-dimensional arrays Java has arrays of arrays int[][] data = new int[3][5]; is shorthand for int[][] data = new int[3][]; data[0] = new int[5]; data[1] = new int[5]; data[2] = new int[5];

Multi-dimensional Arrays in RAM int[][] data = new int[3][5]; Zuuu … out to reality … ArrayRAM.java

Irregular Arrays int[][] weirdData = new int[3][]; weirdData[0] = new int[5]; weirdData[1] = new int[4]; weirdData[2] = new int[7];

Irregular Arrays int[][] weirdData = new int[3][]; weirdData[0] = new int[5]; weirdData[1] = new int[4]; weirdData[2] = new int[7]; weirdData.length == 3 weirdData[1].length == 4 Jioooul … out to reality … ArrayIrreg1.java

Passing Arrays to Methods - 1 Code State of Memory minOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)) { … } A A At before searchMinimum A A arrayOne A. A. Local variable number does not exist before the method execution

Passing Arrays to Methods - 2 Code State of Memory minOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)) { … } arrayOne B B The address is copied at B B number B. B. The value of the argument, which is an address, is copied to the parameter.

arrayOne number While at inside the method C C Passing Arrays to Methods - 3 Code State of Memory minOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)) { … } C C C. C. The array is accessed via number inside the method.

arrayOne number Passing Arrays to Methods - 4 Code State of Memory minOne = searchMinimum(arrayOne); public int searchMinimum(float[] number)) { … } D D arrayOne At after searchMinimum D D D. D. The parameter is erased. The argument still points to the same object.

Example Programs Flunot … out to reality … ArrayParamAvg.java Flunot … out to reality … ArrayParam1.java

Returning Arrays Array variables in methods exist until the method ends, but the array data lives while referenced An array variable can be returned from a method The receiving array variable then refers to the array data, and the array persists Wrrbbrack … out to reality … ArrayReturn.java Wrrbbrack … out to reality … ArrayParam2.java

Local arrays Array variables in methods exist until the method ends The array data referred to by such an array variable is lost and garbage collected when the method ends Dessserts … out to reality … ArrayLocalGC.java