[Array, Array, Array, Array]

Slides:



Advertisements
Similar presentations
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Advertisements

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.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
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.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Arrays After a while. Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need?
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
LCC 6310 Computation as an Expressive Medium Lecture 2.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
1 Intro to Computer Science Arrays Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault University of.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
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.
Arrays [Array, Array, Array, Array] Our Very First Data Structure! Definition - A group of homogeneous elements of a specific data type* Examples: –[a,e,t,w,e,e,y,u,f,v,x,g,h,q,w,r,f,d,a,h,o,a,m,s,z,d]
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Java for Beginners Level 6 University Greenwich Computing At School
Java for Beginners University Greenwich Computing At School DASCO
Manipulating Pictures, Arrays, and Loops part 2
Arrays Low level collections.
Tuples and Lists.
Two Dimensional Array Mr. Jacobs.
Array, Strings and Vectors
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Java for Android is specific
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Engineering Innovation Center
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Arrays, For loop While loop Do while loop
What is an Array? Why Arrays? Programming Examples.
Computers & Programming Languages
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.
Arrays 6-Dec-18.
Sridhar Narayan Java Basics Sridhar Narayan
[Array, Array, Array, Array]
Arrays ICS2O.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Arrays.
CISC181 Introduction to Computer Science Dr
Building Java Programs
CS150 Introduction to Computer Science 1
CSC 142 Arrays [Reading: chapter 12].
Loops and Arrays in JavaScript
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
CS150 Introduction to Computer Science 1
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Suggested self-checks: Section 7.11 #1-11
CS150 Introduction to Computer Science 1
Java: Variables, Input and Arrays
IAT 800 Foundations of Computational Art and Design
Welcome back! October 11, 2018.
LCC 6310 Computation as an Expressive Medium
Arrays Introduction to Arrays Reading for this Lecture:
Variables and Constants
Week 7 - Monday CS 121.
Presentation transcript:

[Array, Array, Array, Array] Arrays [Array, Array, Array, Array]

Our Very First Data Structure! Definition - A group of homogeneous elements of a specific data type* Examples: [a,e,t,w,e,e,y,u,f,v,x,g,h,q,w,r,f,d,a,h,o,a,m,s,z,d] [3.14159, 58008, 0, -123456789, 1000000000000] [Dr. Pepper, Coke, Sprite, Pepsi] [Collie, Dalmatian, Bulldog, Beagle, Rottweiler]

Make Your Own Array!

Declaration An array is declared as follows: Examples: variable_type[] variable_name; Examples: int[] costumerIDs; double[] changeJars; String[] NFLteamNames;

Instantiating An array though is an object so declaring it only gives us a reference to a sequence of primitives or object references that are not created when the array is declared. We must use ‘new’ to create the primitives or object references using the following syntax: variable_name = new variable_type[size];

Array Size variable_name = new variable_type[size]; In this declaration, size is the number of objects or primitives being created. size and must be an integer. Size of an array is set when the array is created(instantiated). Once set, the size of an array can not be changed! There are other data structures though that will let us change the size

Instantiating Examples int[] costumerIDs; costumerIDs = new int[1000]; double[] changeJars; changeJars = new double[6]; String[] NFLteamNames; NFLteamNames = new String[32];

Creating an Array at Declaration We can create an array of primitives at the time that we declare the array in the following way: double [] numbers = {5.2, -8.32, 0.16}; boolean [] values = {true, false, false}; char [] letters = {‘a’, ‘b’, ‘x’}; This type of assignment can only be done when the array is declared. The following is illegal: double [] numbers; numbers = {5.2, -8.32, 0.16};

Indexing Arrays To get an element in an array then write the variable name followed by brackets with an integer in between the brackets: array_varaible[integer] This can be tricky as indexing starts at 0. So in order to reference the nth value in the array then we would put n-1 in the brackets.

Indexing Examples We have the following array: Then double [] numbers = {5.2, -8.32, 0.16}; Then numbers[0] == 5.2 numbers[1] == -8.32 numbers[2] == 0.16 Any integer greater than 2 or less than 0 is out of bounds for this array.

Indexing Activity red green rainbow = [red, orange, yellow, green, blue] rainbow[ 0 ] == ? rainbow[ 3 ] == ? rainbow[ ? ] == blue rainbow[ ? ] == green rainbow[ ? ] == orange red green 4 3 1

Instantiating Arrays of Objects Just like when we declare an objects variables, we must instantiate each object in an array. Example: Toy[] ToyBox; ToyBox = new Toy[2]; ToyBox[0] = new Toy(“Woody”); ToyBox[1] = new Toy(“Buzz”);

Daisy Array Example Step 1 – Declare the array Daisy[] DaisySet; Step 2 – Instantiating Array DaisySet = new Daisy[3]; Step 3 – Instantiate Objects in the Array DaisySet[0] = new Daisy(); DaisySet[1] = new Daisy(); DaisySet[2] = new Daisy();

Iteration We can use loops to cycle through all the elements of an array. The following loop will print all the elements of the array nums assuming there are ten elements for(int i = 0; i < 10; i++){ System.out.println(nums[i]); } Notice that the loop variable i is being used as our index so that as we go through the loop, we reference the next element in the array. Also note that i starts at 0 and goes up to but does not go to 10 which would be out of bounds

Using .length in iteration If the length of the array is not known, we can use .length to obtain the number of elements and use it in our code like so: for(int i = 0; i < nums.length; i++){ System.out.println(nums[i]); } This is especially useful if our loop is to work with arrays of varying lengths

Why use arrays? If we can’t change the size of arrays and there is such a primitive interface, why use arrays? Arrays more closely represent the physical reality of how data is stored on a computer and in general how anything is stored. Example: Books in a Library