Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Programming with Collections Collections in Java Using Arrays Week 9.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Lists in Python.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Single Dimensional Arrays
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 5 Arrays Introducing Arrays
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Arrays in Java.
Chapter 6 Arrays.
Arrays.
Presentation transcript:

Introducing Arrays

Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to create an application to track student progress in a class with 50 students? Creating 50 variables will prove inefficient …

Introducing Arrays  An array might prove a better solution to my problem!  An array is a data structure that contains related items that are of the same data type and where each item in the array shares the same name.  In memory, array values occupy contiguous locations.

Multiple Variables in Memory stu1stu2stu3stu4stu5 stu6 stu7 stu8 stu9 stu10stu11stu12 String stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12;

Printing all the students System.out.println( stu1 ); System.out.println( stu2 ); System.out.println( stu3 ); System.out.println( stu4 ); System.out.println( stu5 ); System.out.println( stu6 ); System.out.println( stu7 ); System.out.println( stu8 ); System.out.println( stu9 ); System.out.println( stu9 ); System.out.println( stu10 ); System.out.println( stu11 ); System.out.println( stu12 ); // How many lines if I wanted to print 200 students?

An Array in Memory fopStu String[ ] fopStu = new String[12]; fopStu[0]fopStu[1]fopStu[2]fopStu[3] fopStu[4]fopStu[5]fopStu[6]fopStu[7]fopStu[8]fopStu[9]fopStu[10]fopStu[11] Reference to the Array Array

Printing all the students in array for(int index = 0; index < 12; index++) { System.out.println( stu[index] ); } // How many lines if I wanted to print 200 students?

Creating an Empty Array in Two Steps A previous slide showed the line of code: String[ ] fopStu; This line is an example of how to create a reference to an array of Strings. This does not create the array object. To create the array we need the following line of code: fopStu = new String[12]; This line is an example of how to create an Array of Strings references. THERE ARE NO STRINGS. Just 12 adjacent memory locations waiting to point at Strings

Creating an Empty Array in a Single Step In general, to create an array of any type, you use the following: Create a reference assign it an Array object. [ ] = new [ ]; Therefore, if you wanted to create an Array to hold 15 Robots, you would type the following: Robot[ ] myRobots = new Robot[15]; This will create a reference to a Robot array that CAN hold 15 individual robots. After this code, NO ACTUAL Robots exist. There are just 15 Robot references in side of the Array.

Creating an Empty Array in Two Steps In some cases you may want to create the array reference, separately from the actual array. You can break the statement into two parts. Robot[ ] myRobots = new Robot[15]; would become: Robot[ ] myRobots; myRobots = new Robot[15]; //myRobots refers to an array that can hold 15 Robots Since arrays are object, the reference could later be used to “point” to a new array. In other words, the reference could be reassigned. myRobots = new Robot[25]; //myRobots now refers to an new array that can hold 25 // Robots the old Array and all its contents are now gone

Assigning Values to an Array  To assign a value to an array (after it has been created), you need to call the array by its name and then provide a subscript to store the new value (the index number goes in a pair of square brackets): String [] fopStu = new String [12]; String [] fopStu = new String [12]; fopStu[4] = "Jacob"; fopStu[5] = “Sally”; Robot [] myRobots = new Robot[4]; Robot [] myRobots = new Robot[4]; myRobots[3] = new Robot(1,1,North,0);

The array length field  Once an array is created, its size is fixed. It cannot be changed. The argument in the [ length ] fixes the size of the array when it is created.  We can find how many elements an array has by accessing the length field.  You can find its size of an array int numStu = fopStu.length;  length is always a nonnegative integer value: Based on previous slides, this would assign the value 12 into the variable numStu.

An Array in Memory(again) fopStu String[ ] fopStu = new String[12]; fopStu[0]fopStu[1]fopStu[2]fopStu[3] fopStu[4]fopStu[5]fopStu[6]fopStu[7]fopStu[8]fopStu[9]fopStu[10]fopStu[11] Note that each “cell” of the array has a separate index value. The smallest index value will ALWAYS be 0, and the largest will ALWAYS be one less than the length of the array. If the length is 12, then the max index is 11.

Initializing Arrays int[] myList = new int[5]; for (int i = 0; i < myList.length; i++) myList[i] = i * i; myList[i] = i * i; Would create the following array: myList myList[0]myList[1]myList[2]myList[3]myList[4]

Initializing Arrays Robot[] myBots = new Robot[3]; for (int i = 0; i < myBots.length; i++) myBots[i] = new Robot(1,i+1,North,0); myBots[i] = new Robot(1,i+1,North,0); Would create the following array: myBots myBots[0]myBots[1]myBots[2] (1,1) Facing North 0 Beepers (1,2) Facing North 0 Beepers (1,3) Facing North 0 Beepers

Array Elements & Subscripts  An element is an object referenced in an array. Java stores each element in a different position in an array.  We reference a position using a subscript (a.k.a. index).  To reference an element, we need to provide the array's name and the element's subscript: myBots[0].move();  Each element in a array behaves exactly like a reference of the same type.

Creating an Array with Initial Values When we are declaring an array, we can supply specific values in a comma-delimited list: String[ ] students = {"James","Ravi","Mary”,”Jackson“}; int[ ] stuIds = { 8752, 9062, 6087, 8745, 2354, 1298}; Robot [] robs = { new Robot(1, 1, North, 3), new Robot(1, 2, North, 3), new Robot(1, 2, North, 3), new Robot(1, 3, North, 3), new Robot(1, 3, North, 3), new Robot(1, 4, North, 3)}; new Robot(1, 4, North, 3)};

Creating an Array with Initial Values (con’t) double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5};

Accessing Array Values  To access a value from an array, call the array by name and indicate which subscript you want to retrieve (the index number goes in a pair of square brackets): String name = fopStu[3]; int id = stuIds[3]; Robot karel = myBots[2];

Copying Arrays A simple assignment cannot copy arrays. Since an array is an object, if you try to use the assignment operator (=) you will end up with two references to the same array. If myList is the following array: myList myList[0]myList[1]myList[2]myList[3]myList[4] then, int[] anotherName; anotherName = myList; will result in: myList myList[0]myList[1]myList[2]myList[3]myList[4] anotherName anotherName[0]anotherName[1]anotherName[2]anotherName[3]anotherName[4] The statement: anotherName[3] = 5; would also effect myList[3]

Traversing Arrays  To traverse an array means to “visit” each cell in order, doing whatever the job may be.  for loops do a VERY good job of this. BeeperSweeper [] sweepers = new BeeperSweeper[9];... // code to initialize all 9 cells with sweeper in // appropriate locations... for(int i = 0; i < sweepers.length; i++) sweepers[i].task(); This code will invoke each sweepers task method, without having to name 9 different robots and use 9 lines of code to invoke the task methods. Would the for loop still work if the array were created and initialized with a different number of BeeperSweepers?

Sample Algorithms  Summing/accumulating  Finding largest  Finding smallest  Searching  Sorting by ….

Summary  Arrays are data structures that can hold multiple values at the same time.  The values in an array are called elements and they share the same name (the array's name), but are accessed through unique subscripts (indicies). continued …

Summary  Subscript (index) numbering always begins with zero and goes up to the length of the array-1.  The size of an array can be found by using the length field.  When arrays are created, they are initially empty. If you want them to hold something, YOU must put it in. continued …

Summary  for loops are very useful in traversing an array.  Arrays are object, and therefore cannot be duplicated with the assignment (=) operator.

Assignment Part 2 *  From the CarDealer Project, complete the Dealer class. You will need to implement the following: You will need to implement the following: public boolean find(Car c)public boolean find(Car c) public double getInventoryValue()public double getInventoryValue() public double getAverageCarValue()public double getAverageCarValue() public Car getMostValuableCar()public Car getMostValuableCar() public Car getLeastValuableCar()public Car getLeastValuableCar() public void sortCarsByYear()public void sortCarsByYear() public void sortCarsByValue()public void sortCarsByValue() *See the IntroObjects PowerPoint for Asignment Part 1 Help for the above methods can be found in the ArrayAlgorithms PowerPoint