Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

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.
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Arrays.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Introduction to arrays Data in economy size packages.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Chapter 9 Introduction to Arrays
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Yong Choi School of Business CSU, Bakersfield
An Introduction to Java – Part I, language basics
Single-Dimensional Arrays chapter6
Arrays 3/4 June 3, 2019 ICS102: The course.
Presentation transcript:

Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods for arrays 4. Declaring and using arrays 5. Example code 6. Practice problems 7. Answers

Arrays An array is an object which are dynamically created. An array can hold values, and these values, or elements, are stored at a specific location known as the index.

Terms 1.Index 2.Element 3.Array length 4.Logical size 5.Physical size

Index Arrays are made of a grouping of values that are in a specific order, this order starts at 0 and each succeeding value increases by one integer value meaning that the final value will have a location of n-1 where n is equal to the size of the array. The specific location of each value is called the index

Elements Elements are the values that are stored in each index. For instance, an element can be the integer 1, the string “Hello World”, the character ‘a’, or any other value type.

Array Length The array length is the number of indices in the specified array. This concept can be thought of in a couple ways. The length can be the index number of the last array plus one. This can be useful when you are given just the index number of the last array. Or you can simply use the length() method which will return the length of the array. This can be very useful when you are using loops and want to stop at the end of the array.

Logical Size The logical size of an array is the number of indices in the array that are being used. Therefore, if array1[] has memory allocated to store 5 values and it only contains 2 values, it has a logical size of 2. This is analogous to the egg carton below. While it has room for many eggs, it is only using a limited amount of holes.

Physical Size The physical size is the total number of values that an array is able to hold. This means that even if array1[] has memory allocated for 5 values, but it is empty, its physical size is 5. This is analogous to the egg carton below because even though it has room for many eggs, it doesn’t have to fill them to be able to hold 12 eggs

One Dimensional Arrays A one dimensional array is a single array in which each element can be located in a one dimensional location. A one dimensional array can be compared to graphing on just the X axis.

Two Dimensional Arrays A two dimensional array can be thought of as an array of an array. Elements of a two dimensional array can be located by using rows and columns. A two dimensional array can be compared to graphing using the X axis and Y axis. The syntax for declaration and allocation for a two dimensional array is int[][] a2 = new int[10][5];

Ragged Arrays Arrays can have different numbers of elements in a row. These arrays are called ragged arrays

Parallel Arrays Parallel arrays are one dimensional arrays that have elements that correspond to elements in the other array in the same index. An example of this would be a phone directory in which a name corresponds to a phone number

Methods for Arrays Click here to view methods that can be used with arrays

Declaring Arrays In order to use an array you must first declare the variable that will be used to refer to the array. The syntax for declaring a one dimensional array is int[] array1; After that, you must create the array, the syntax for that is array1 = new int[10]; This also allocates memory for the array.

Example Code Following this slide is an example of how arrays can switch contents. This code demonstrates the declaration of arrays, allocating memory for each array, and setting values to indices of the array.

Example Code public class ArraySwitch { public static void main(String[] args) { int[] array1; int[] array2; int[] temp; array1 = new int[3]; array2 = new int[3]; temp = new int[3]; array1[0] = 0; array1[1] = 1; array1[2] = 2; array2[0] = 10; array2[1] = 11; array2[2] = 12; temp[0] = array1[0]; temp[1] = array1[1]; temp[2] = array1[2]; array1[0] = array2[0]; array1[1] = array2[1]; array1[2] = array2[2]; array2[0] = temp[0]; array2[1] = temp[1]; array2[2] = temp[2]; }

int[] array1; int[] array2; int[] temp; array1 = new int[3]; array2 = new int[3]; temp = new int[3]; This is the part of the code that first declares the arrays and then allocates memory for each of the arrays. array1[] and array2[] are the two arrays that will swap one another’s elements, temp[] is the array that will temporarily hold the elements of array[]1 while array1[] is receiving the elements from array2[]. Since each array will need to hold 3 integer values the code sets limit of the capacity of the arrays to 3.

array1[0] = 0; array1[1] = 1; array1[2] = 2; array2[0] = 10; array2[1] = 11; array2[2] = 12; This code sets values to each of the indices of array1[] and array2[]. Remember that when using arrays, you start counting at 0, so the arrays we are using which hold three values will have a maximum index number of 2.

temp[0] = array1[0]; temp[1] = array1[1]; temp[2] = array1[2]; array1[0] = array2[0]; array1[1] = array2[1]; array1[2] = array2[2]; array2[0] = temp[0]; array2[1] = temp[1]; array2[2] = temp[2]; This code is the code responsible for swapping the values of array1[] with array2[]. Even though each array can only hold a maximum of three values, they are able to change those values after they have been assigned.

Question One Which of the following methods would be used to display the contents of an array. The answer choices will be on the next three slides.

Answer A public static void dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ System.out.print(arrays[i]); }

Answer B public static int dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ return arrays; }

Answer C public static void dispArray(int[] arrays) { int i = 0; for(; i < 4; i++){ System.out.print(arrays[i]); }

Answer One The correct answer is A. B is not correct because arrays[] is not compatible with the integer type method. C is not correct because it tries to display the element of an index that is not defined for the array

Question Two After the following code has been run, what is true? int array1[] = new int[10]; A.array1[0] is undefined B.array1[10] is undefined C.the code will not compile D.array1[6] = 0

Answer Two The correct answer is B and D B is correct because since the array has 10 indices and it starts with 0 array1[10] is an undefined location. D is correct because 0 is the default value for an int value

Code Problem When the people in charge of making the school directories for Upper Dublin, Java, and Silicon Valley they accidentally entered the phone numbers for Java into the Upper Dublin Directory, the Upper Dublin phone numbers into the Silicon Valley directory and the Silicon Valley phone numbers into the Java directory

UD –Alex –Kevin –Ben –Matt –Andrew SV Chip IC Sandy Bill Alan Java Ari Eva Charles Byron James

Write a program that creates and displays three parallel arrays, one for each district (After the corrections have been made). Make a method called arraySwap() that will swap arrays’ contents in order to put the correct information in the correct array.