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.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Chapter 10 Introduction to Arrays
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
Introduction to arrays. Array Homogeneous collection of components stored in adjacent memory locations –All elements share same data type –Entire collection.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
©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.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
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.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
 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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall The index of the first position in an.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Java Programming Language Lecture27- An Introduction.
Windows Programming Lecture 03. Pointers and Arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Computer Organization and Design Pointers, Arrays and Strings in C
Primitive and Reference Data Values
Advanced Programming Behnam Hatami Fall 2017.
Basic Files + (Chapter 10 – Arrays) : Objectives
Chapter 6: Arrays.
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6: Arrays.
Lecture 18 Arrays and Pointer Arithmetic
Programs and Classes A program is made up from classes
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,
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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 you? An array is a collection of data values of the same data type. If your program needs to deal with 100 integers, 365 real numbers, etc., you will use an array.

Arrays Variables are Reference Variables Primitive variables contain values Reference variables point at objects byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type

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.

Arrays of Primitive Data Types Array Declaration [ ] //variation 1 [ ] //variation 2 Array Creation = new [ ] Example double[] rainfall; rainfall = new double[12]; Variation 1 double rainfall [ ]; rainfall = new double[12]; Variation 2 An array is like an object!

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

Example Programs Zadoot … out to reality … ArrayElements1.java Zadoot … out to reality … ArrayElements2.java

Array Lengths double[] rainfall = new double[12]; double annualAverage; double sum = 0.0; int index; for (index = 0; index < rainfall.length; index++) { rainfall[index] = keyboard.nextDouble(); sum += rainfall[index]; } annualAverage = sum / rainfall.length; The public constant length returns the capacity of an array. 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, arrays can be declared and initialized at the same time. int[] number = { 2, 4, 6, 8 }; double[] samplingData = { 2.443, 8.99, 12.3, , 18.2, 9.00, 3.123, , }; The capacity of the array is set to the number of elements in the list. number.length samplingData.length 4 9 Groeeet … out to reality … ArrayInit.java (Secretly, Java pre-initializes arrays to the default value for the type, e.g., 0 for int arrays.)

References are Pointers A reference variable points to an object So, arrays are like objects, but don't worry about that for now – we’ll come back to it. But it does mean you can Lose an array Have an array variable pointing nowhere Have multiple references to an array Not copy an array with =

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

The array to no-where An array variable can be explicitly made to point to no data, using the null value A null array is different from an array of length zero. Spaaocie … out to reality … ArrayNULL.java

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

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

Declaring and Creating a 2-D Array Declaration [][] //variation 1 [][] //variation 2 Creation = new [ ][ ] Example double[][] payScaleTable; payScaleTable = new double[4][5];

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 … ArrayRAMMatrix.java Zuuu … out to reality … ArrayRAMCube.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

Irregular Arrays … null int[][] data = new int[3][]; data[0] = new int[5]; data[1] = new int[4]; data[2] = null; Jioooul … out to reality … ArrayIrreg2.java

Multi-dimensional Array Initialization Like 1D arrays, it is possible to declare and initialize a multi-dimensional array at the same time. Frong … out to reality … ArrayMDInit.java Frong … out to reality … ArrayCubeInit.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 … 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