SSS 2.1 – Huntsville 2/22/14 Carol Yarbrough Alabama School of Fine Arts Arrays.

Slides:



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

Review Generics and the ArrayList Class
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Implements the List Interface  What is an interface?  all abstract methods  can not have instance variables  List is an interface  ArrayList implements.
Arrays.
Preliminaries Attendance sheets –I remembered! HW1 due tonight –progress report Stage 1 due on Friday –progress report.
Lecture 12: Midterm Review Multiple choice quiz from pub website (see web page for link and use access code liang6e614 ) Homework 5: N Queens Sample midterm.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Alice in Action with Java
1 Array Lists Pat Phillips Craig High School Janesville, Wisconsin 2005.
Building Java Programs
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
Stacks, Queues, and Deques
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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.
Multiple Choice Solutions True/False a c b e d   T F.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
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 Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays and ArrayLists. int numbers[] = new int[10]; numbers Other ways to declare the same array 1) int[] numbers = new.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
List Interface and Linked List Mrs. Furman March 25, 2010.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
int [] scores = new int [10];
JAC444: Intro to Java Arrays and Vectors Tim McKenna
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lecture 20: Wrapper Classes and More Loops
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Game Extras Pepper.
ARRAYLIST AND VECTOR.
Cse 373 April 24th – Hashing.
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Array List Pepper.
Review of Classes and Arrays
ArrayLists.
Grouped Data Arrays, and Array Lists.
Review of Classes and Arrays
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
First Semester Review.
Presentation transcript:

SSS 2.1 – Huntsville 2/22/14 Carol Yarbrough Alabama School of Fine Arts Arrays

What is an array? Sometimes we think of as a primitive – but is really an Object of type type[] An array inherits from Object 11 methods (equals, toString, …) equals & toString are NOT overriden !!

Advantages Can store: Primitives / arrays Object references More efficient than ArrayList when using primitives Uses less storage due to lack of overhead Can have multi-dimensional arrays int[][] m = new int[2][3]; Creates an array of length 2. Each entry is an array of 3 ints. m[i][j] is an int m[i] is an int[]

Disadvantages can’t be resized have to copy into a new array Hard to insert or remove in middle of array

Defining Arrays public class arrayExample { private char[] A = { 't', 'p', 'd', 'a', 'm' }; private String[] B; public arrayExample() { B = new String[5]; B[0] = "tomato"; B[1] = "pear"; B[2] = "durian"; B[3] = "apple"; B[4] = "mango"; }

Populating Arrays private int[] card = new int[10]; // refer to element by index card[0] = 4; card[i] = 995;

Accessing private int[] card = new int[10]; // refer to element by index int firstCard = card[0]; int newCard = card[i]; Finding Length int clen = card.length;

Array Traversals

For Loop, While Loop, For Each Loop? Could you do this with a for-each loop?

Where For Each Loop is Used int [] rolls = new int[10]; for(int i = 0; i < rolls.length ; i++) rolls[i] = (int) (Math.random() * 6) + 1; int sumRolls = 0; for (int r : rolls) { sumRolls = sumRolls + r; } Remember r is a reference, not an index. Can not change reference in for each loop.

For Each Loop Can’t be used to change array object! int[] a = {1, 2, 3}; for (int v : a) v = 6;// Does not change // values in array a. NOTE: v is a reference value, not an index ! Can be used to mutate referenced objects! Bug[] bugs = {new Bug(), new Bug(), new Bug()}; for (Bug b : bugs) b.setColor(Color.GREEN);// bugs now green

Copy Elements From One Array to Another

Circular Traversal of an Array

Work the Multiple Choice Questions in Your Student Packet

Multiple Choice Answers 1.C 2.E 3.B 4.D 5.D 6.E

Sample Free Response

Sample Free Response Solutions Part A private int getChargingCost(int startHour, int chargeTime) { int cost = 0; int index = startHour; for (int k = 1; k <= chargeTime; k++) { cost += rateTable[index]; index = (index + 1) % rateTable.length; } return cost; }

Part B public int getChargeStartTime(int chargeTime) { int startTime = 0; int minCost = getChargingCost(startTime, chargeTime); for (int k = 1; k < rateTable.length; k++) { int cost = getChargingCost(k, chargeTime); if (cost < minCost) { startTime = k; minCost = cost; } return startTime; }