CMSC 150 ARRAYS CS 150: Fri 10 Feb 2012. Motivation  Consider a list of your contact addresses: String Zero = String.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 8: Arrays.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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.
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.
Array Yoshi. Definition An array is a container object that holds a fixed number of values of a single type. The length of an array is established when.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
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 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. 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.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
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.
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.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
CMSC 150 INTRODUCTION TO COMPUTING CS 150: Fri 13 Jan 2012.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
CS177 Week2: Recitation Primitive data types and Strings with code examples.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
CMSC 150 MULTI-DIMENSIONAL ARRAYS CS 150: Mon 27 Feb 2012.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Chapter 1 Primitive Java. import java.io.*; /* an example program */ public class Example1 { public static void main (String[] args) { System.out.println("Hello.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
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];
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
String & Exception. Lesson plan Class & Object String Exercise for midterm.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
AKA the birth, life, and death of variables.
using System; namespace Demo01 { class Program
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
CS Week 8 Jim Williams, PhD.
Decision statements. - They can use logic to arrive at desired results
An Introduction to Java – Part I, language basics
Code Animation Examples
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Arrays in Java.
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
More on iterations using
Presentation transcript:

CMSC 150 ARRAYS CS 150: Fri 10 Feb 2012

Motivation  Consider a list of your contact addresses: String Zero = String One = String Two = String Three = String Four = … String EightySix = … String EightSixSevenFiveThreeOhNine =

Array  data structure to store multiples of same type of data  can be primitive or class type  S yntax:  type[] variableName;  type[] variableName = new type[size];  Examples: int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ];

public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers null

public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]

public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]

public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]

public class ArrayExample { public static void main(String[] args) { int[] cardNumbers; cardNumbers = new int[3]; cardNumbers[0] = ; cardNumbers[1] = ; cardNumbers[2] = ; } In Memory cardNumbers 0x12AB79 [0] [1] [2]

In Memory strings null public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); }

In Memory strings 0x33DB20 [0] [1] null public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); }

In Memory strings 0x33DB20 [0] [1] null mySecondChar myLength myFirstChar ‘Z’ ‘a’ 2 char chartAt(int index) int length() void printString() … public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); } 0x33DB26

In Memory strings 0x33DB20 [0] [1] mySecondChar myLength myFirstChar ‘Z’ ‘a’ 2 char chartAt(int index) int length() void printString() … public class ArrayExample { public static void main(String[] args) { SimpleString[] strings; strings = new SimpleString[2]; strings[0] = new SimpleString(‘Z’,’a’); strings[1] = new SimpleString(‘O’,’y’); } 0x33DB26 0x33DC01

The length field  A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; //

The length field  A variable (not method!) indicates array length int[] cardNumbers = new int[10]; boolean[] missingTeeth = new boolean[32]; String[] contacts = new String[ ]; int howManyCards = cardNumbers.length; // 10 int hillbillyFactor = missingTeeth.length; // 32 int numContacts = contacts.length; // int howManyCards = cardNumbers.length(); int hillbillyFactor = missingTeeth.length(); int numContacts = contacts.length(); Will not compile! For arrays, length is not a method. Will not compile! For arrays, length is not a method.

Managing Arrays  Typically use for loops: int[] randomNumbers = int[1024]; for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); }

Managing Arrays  Typically use for loops: int[] randomNumbers = int[1024]; for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = generator.nextInt(10000); } int numberOfOdds = 0; for (int i = 0; i < randomNumbers.length; i++) { if ( randomNumbers[i] % 2 != 0 ) { numberOfOdds++; }

Let’s Try Some…