CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
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.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Introduction to Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introduction to Java Java Translation Program Structure
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
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.
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];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Chapter 2 Clarifications
CSC111 Quick Revision.
Chapter 8 – Arrays and Array Lists
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Chapter 7 Part 1 Edited by JJ Shepherd
Introduction to Methods in java
Repetition.
Something about Java Introduction to Problem Solving and Programming 1.
Visual Basic .NET BASICS
Yong Choi School of Business CSU, Bakersfield
Java Language Basics.
Topics discussed in this section:
CS2011 Introduction to Programming I Arrays (I)
Arrays in Java.
Consider the following code:
More on iterations using
Week 7 - Monday CS 121.
Presentation transcript:

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17

What if… What if we needed to track the grades for 20 students, the GPAs for students, the balance for 10M checking accounts? – one variable for each “thing” we need to track? Technically, it would be possible to do it this way. Ridiculously difficult – Extensibility? – Ability to perform a simple calculation on 10M accounts such as Pay interest might take 10M lines of code – one line for each calculation. – Making a deposit would require 10M if statements. Simply impractical

Arrays Allow programmer to refer to more than one object of the same type by using one name/identifier and an index. – index can be a variable – lend themselves well to use with loops

Declaration Declaring an array – arrays are objects in Java – Create a reference variable to an array by using [] – instantiate an array object using new [size of array] EXAMPLE: int [] grades; grades = new int[10]; OR int[] grades = new int[10]; grades [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Declaration Size can be input by the user. Scanner s = new Scanner(System.in); int size = 0; int [] data; System.out.print(“Please enter the number of students: “); size = s.nextInt(); data = new int [size]; //and so on

Characteristics of Arrays Zero subscripted – first slot (called element) has index of zero. – last element has index of (size – 1) Homogeneously Typed – Every element store in array has to be of the same data type Cannot change size once instantiated Can tell you their size using data member length – int [] data = new int[1001]; System.out.println( data.length );//prints 1001

Accessing Elements of Array Use an index to access the elements. – can be a literal or variable int [] data = new int [20]; data[0] = 98; data[1] = 20; int [] data = new int[20]; for (int i = 0; i < data.length; i++) data[i] = i + 2; Literal variable

Array Example //Declare each element with the square of its index. //e.g. [2] would contain 4, [3] would contain 9, etc. public static void main (String [] args) { int [] data = new int [20]; for (int i = 0; i < data.length; i++) data [i] = i * i; for (int i = 0; i < data.length; i++) System.out.println(“data[“ + i + ”] = “ + data[i]); }

BREAKOUT 1

On the Board: Searching or an Element

On the Board: Find Max Element

Arrays of Object References: Review Remember Student stu1; only declares a reference variable to a Student object. – no actual Student object yet. stu1 ?

Arrays of Object References Student [] class = new Student[10]; Creates and array of 10 Student references. There are no student objects yet. for (int i = 0; i < class.length; i++) class[i] = new Student(); Instantiates a new object for each element of the array to point to.

Arrays of Object References Access public interface of object from array element just like normal. System.out.println(class[2].getName()); class[3].setGPA(3.99); Acts like a typical Student reference variable