Arrays (Part 1) Computer Science Erwin High School Fall 2014.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
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.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Chapter 7 & 8- Arrays and Strings
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSE 131 Computer Science 1 Module 1: (basics of Java)
Java Unit 9: Arrays Declaring and Processing Arrays.
Hello AP Computer Science!. What are some of the things that you have used computers for?
DAT602 Database Application Development Lecture 5 JAVA Review.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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 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 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 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
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.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Comp1004: Programming in Java I Variables - Primitives, Objects and Scope.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Arrays Declare the Array of 100 elements Notes
Organizing Memory in Java
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.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Building Java Programs
Building Java Programs Chapter 2
Chapter 2 Programming Basics.
Arrays in Java.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Arrays (Part 1) Computer Science Erwin High School Fall 2014

How to Use the Slides As you go through the slides, answer the questions that appear on a separate piece of paper. You will turn this paper in when you complete the slideshow. If you have questions as you read, list these on your paper as well.

Something to Think About We have learned a lot of useful tools in our coding practice: –Using variables to store a piece of information: numbers, text, truth values (true or false), etc. –Conditional statements to only run code in certain situations (if-else statements) –Loops to run blocks of code multiple times or if certain conditions are met (while, do-while, for loops, etc.)

Something to Think About But what if we want to store a lot of information? What if we wanted a list of all the student id numbers at our school? Or a list of all the student names? Or phone numbers? Or addresses? Or GPAs?

Can we store information in a list or something?

We can… using ARRAYS.

What is an Array? An array is a simple but powerful programming language construct used to group and organize data. It is a list of values, where each value is stored at a specific numbered position in the array.

What is an Array? The number corresponding to each position is called an index. Array indexes always begin at zero, just like indexes for the characters that make up a String.

Accessing Information To access a value stored in an array, we use the name of the array followed by the index in square brackets.

Array Example Suppose we wanted to store the heights of 11 different students in a class. We could create an array of integer values to store the heights of the student (in inches).

Array Example height

Array Example height index Value of height[5]

Problem height Consider the following code segment: int x = height[8]; System.out.println(x); What is the output?

Problem height Consider the following code segment: int x = 6; System.out.println(height[x]); What is the output?

Problem height What are each of the following? –a. height[2] + height[5] –b. height[2 + 5] –c. height.length

Declaring & Using Arrays In Java, arrays are objects. To create an array, we must declare an array just like other objects.

Array Declaration The array is instantiated using the new operator, which allocates (or sets aside) memory space to store the values. The following code represents the declaration for the student height array (for 11 students). int[] height = new int[11]; Notice the braces after int. This tells the computer we are creating an array of integers and naming the array height.

Array Declaration The array is instantiated using the new operator, which allocates (or sets aside) memory space to store the values. The following code represents the declaration for the student height array (for 11 students). int[] height = new int[11]; The 11 inside the second set of braces is to tell the computer how many values will be stored in the array.

Array Elements A value stored in an array is sometimes called an array element. The type of values that an array holds is called the element type of the array. Arrays can hold primitive types (int, double, boolean, etc.) or objects from any other class (String, Ball, Card, Student, etc.)

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); }

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } An array of integers is created and named list.

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } It will hold 15 total values.

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } It will hold 15 total values

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } For each index value from 0 to 14 (index < 15) the array element at that index will contain the value of index *

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } For each index value from 0 to 14 (index < 15) the array element at that index will contain the value of index *

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } The element at index 5 is then set equal to

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } The element at index 5 is then set equal to

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } The for loop here is structured different from ones we have seen before. This is specifically for an array of values or objects

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } For each integer element in list this loop will print out the number. The number is stored in the variable value each time through the loop. As soon as the end of the list is reached, the loop stops

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15]; for (int index = 0; index < 15; index++) list[index] = index * 10; list[5] = 999; for (int value: list) System.out.print(value + “ “); } Output:

Problem 4 What is the output? int[] nums = {2, 3, 5, 1, 0, 6, 7}; System.out.println(nums[0]); System.out.println(nums[2]); System.out.println(nums[5]);

Problem 5 What is the output? int[] nums = {2, 3, 5, 1, 0, 6, 7}; System.out.println(nums[1+3]); System.out.println(nums[7/2]); System.out.println(nums[6]);

Problem 6 What is the output? double[] nums = new double[10]; nums[0] = 10.5; nums[3] = 98.6; nums[2] = 77.5; System.out.println(nums[0]); System.out.println(nums[3]); System.out.println(nums[7]);

Problem 7 What is the output? String[] words = new String[10]; words[0] = “dog”; words[3] = “cat”; words[2] = “pig”; System.out.println(words[0]); System.out.println(words[3]); System.out.println(words[7]);

Problem 8 What is the output? int[] nums = {3, 2, 5, 1, 0, 6} for (int loc = 0; loc < nums.length; loc++) { System.out.println(nums[loc]); }

Problem 9 What is the output? int[] nums = {3, 2, 5, 1, 0, 6} for (int item: nums) { System.out.println(item); }

Problem 10 What is the output? String[] words = {“cat”, “pig”, “dog”} for (String animalName: words) { System.out.println(animalName); }

Program 1 Create a class file called PlayingCard. It should have two properties – a String to hold the suit and an int to hold the value. It should also have five methods: –getSuit, setSuit, getValue, setValue and toString (that prints out the suit and value)