02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)

Slides:



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

UNIT IV.
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.
Arrays.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to arrays Data in economy size packages.
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.
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.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Programming with Java, for Beginners Arrays of Objects.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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 Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
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.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Intro to Computer Science Arrays Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault University of.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
ARRAYS Multidimensional realities Image courtesy of
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Array in C# Array in C# RIHS Arshad Khan
Objects First with Java CITS1001
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.
Arrays November 8, 2017.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Arrays .
Chapter 7 The Java Array Object © Rick Mercer.
Arrays ICS2O.
Arrays in Java.
Variables and Computer Memory
Week 7 - Monday CS 121.
Presentation transcript:

02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)

02/09/2005CSE 1101 public class Frog{ private boolean formerPrince; private String phrase1; private String phrase2; private String phrase3; private String phrase4; private String phrase5; private String phrase6; private String phrase7; private String phrase8; private String phrase9; private String phrase10;... What if our Frog (from homework 2) could say 10 different things?

02/09/2005CSE 1102 public class CrashCop{ private Taxi taxi1; private Taxi taxi2; private Taxi taxi3; private Taxi taxi4; private Taxi taxi5; private Taxi taxi6; private Taxi taxi7; private Taxi taxi8; private Taxi taxi9; private Taxi taxi10; private Taxi taxi11; private Taxi taxi12; private Taxi taxi13; private Taxi taxi14; private Taxi taxi15; What if our CrashCop (from homework 3) could watch 15 taxis?

02/09/2005CSE 1103 public class Sitter{ private Sitter kid1; private Sitter kid2; private Sitter kid3; private Sitter kid4; private Sitter kid5; private Sitter kid6; private Sitter kid7; private Sitter kid8; private Sitter kid9; pirvate Sitter kid10; private Sitter kid11; private Sitter kid12; private Sitter kid13; private Sitter kid14; private Sitter kid15; private Sitter kid16; private Sitter kid17; private Sitter kid18; private Sitter kid19; pirvate Sitter kid20; What if our Sitter (from homework 4) could watch 20 kids?

02/09/2005CSE 1104 public class IToonList{ private IToon toon1; private IToon toon2; private IToon toon3; private IToon toon4; private IToon toon5; private IToon toon6; private Ttoon toon7; private IToon toon8; private IToon toon9; private Ttoon toon10; private IToon toon11; private IToon toon12; private IToon toon13; private IToon toon14; private IToon toon15; private IToon toon16; private Ttoon toon17; private IToon toon18; private IToon toon19; private Ttoon toon20; private IToon toon21; private IToon toon22; private IToon toon23; private IToon toon24;... What if we want to store 500 Itoons (from homework 2)?

02/09/2005CSE 1105 What if we want to store lots of things… …but we don’t want to declare a separate variable for each one? That’s what arrays are good for.

02/09/2005CSE 1106 int[] data; data = new int[5]; data[0] = 6; data[1]= 10; data[2] = 12; What is an Array? It’s an easy way to declare lots of variables that all have the same type.

02/09/2005CSE 1107 Array Elements and Indices The “data” array has 5 elements: data[0] whose value is 6 data[1] whose value is 10 data[2] whose value is 12 data[3] whose value is 0 data[4] whose value is 0 When accessing an element, the number within square brackets is called an index The valid indices are 0 thru (array length - 1)

02/09/2005CSE 1108 int[] data; data is a reference variable whose type is int[], meaning “array of ints”. At this point its value is null. data = new int[5]; The new operator causes a chunk of memory big enough for 5 ints to be allocated on the heap. Here, data is a assigned a reference to the heap address. data[0] = 6; data[1]= 10; data[2] = 12; Initially, all five ints have the value 0. Here, three of them are assigned other values. int[] info = {6, 10, 12, 0, 0}; // another way An Array is an Object (with unique [] syntax)

02/09/2005CSE 1109 int x = data[0]; data[3] = data[2]; data[4] = sum(1,5); data[4] = sum(1, data[4]); System.out.println(“data[4] is “ + data[4]); x = data[0] * data[1] * data[2]; Person p = new Person; p.setAge(data[3]); Operations on Array Elements An “element” of an array of ints can be used virtually anywhere an expression of type int is valid. Likewise for arrays of other types.

02/09/2005CSE int[] data; data = new int[5]; data[0] = 6; data[1]= 10; data[2] = 12; int result = 0; // the value of data.length is 5 for (int i =0; i < data.length; i++){ result = result + data[i]; } An Array’s “length” Instance Variable

02/09/2005CSE double[] temps; temps = new double[24]; temp[0] = 78.5; temp[1] = 84.2; boolean[] answers = new boolean[6]; answers[0] = true; if (answers[0) foo(); char[] buffer = new char[500]; open a file for reading while (more chars in file & buffer not full) buffer[i++] = char from file Arrays of Other Primitive Types