1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

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.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Fundamental Programming Structures in Java: Strings.
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, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
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.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
ArrayList, Multidimensional Arrays
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.
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.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CS107 References and Arrays By Chris Pable Spring 2009.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
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.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
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];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter VII: Arrays.
Foundations of Programming: Arrays
Object Oriented Programming in java
Module 4 Loops.
Grouped Data Arrays, and Array Lists.
Peer Instruction 4 Control Loops.
LCC 6310 Computation as an Expressive Medium
Review for Midterm 3.
Week 7 - Monday CS 121.
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS

2 OBJECTIVES! Understand array basics Construct an array Initialize an array with user-provided values Iterate over an array

3 LET’S GET STARTED! In your notebook… 1. How is an array constructor different from the other constructors we’ve seen (e.g. for Scanner)? 2. We’ve actually seen an array before, but it was a part of a “magic incantation”. Where was it?

4 WHAT IS AN ARRAY? An array is a fixed-size list of items of a particular type. It is zero-indexed, just like a String. It is an object, not a primitive type, so it has to be constructed before it can be used: When a new array is constructed, all of the items are automatically initialized to zero (or a zero-equivalent value, like false or null ). int[] myArray = new int[20];

5 ALTERNATE CONSTRUCTION In addition to using new, you can provide an array initializer much like initializing a String to a string literal: Remember to start and end the list with curly braces! String myString = "hello!"; int[] myArray = {8, 42, 13, 77, 8, 16}; // myArray has 6 elements, just like doing: // new int[6]

6 USING AN ARRAY… Since an array has a fixed size, Java provides a way to find out its length. This is almost the same as getting the length of a String, with one important difference. What’s the difference? You don’t put parentheses after the “.length” when getting the length of an array: String myString = "hello!"; int[] myArray = {8, 42, 13, 77, 8, 16}; int strLen = myString.length(); // evaluates to 6 int arrLen = myArray.length; // evaluates to 6

7 USING AN ARRAY… To reference a specific element in the array, provide the index (remember it’s zero-based!) in square brackets after the array variable. This is similar to charAt() on a String: String myString = "hello!"; int[] myArray = {8, 42, 13, 77, 8, 16}; char c = myString.charAt(1); // c gets 'e' int n = myArray[1]; // n gets 42

8 ARRAYS AS PARAMETERS… When you pass an array as a parameter to a method, you actually pass a reference to it. This is because the array is an object, not a primitive type. This means that a method can change the elements of an array. This is very different than the behavior with primitive types and takes some getting used to!

9 ARRAYS AS PARAMETERS… What does this display? public static void main() { int x = 1; int[] y = {1}; changeValues(x, y); System.out.println("x => " + x + ", y[0] => " + y[0]); } public static void changeValues(int a, int[] b) { a++; b[0]++; } x => 1, y[0] => 2 // does change the element in the caller!

10 FOR LOOPS AND FOR-EACH LOOPS We already know about for loops, and those work just as they always have. Java also has a special loop for arrays if all you want to do is do something with each element value: the “for-each” loop: Read this as “for each ‘i’ in myArray…” int[] myArray = {8, 42, 13, 77, 8, 16}; for (int i : myArray) { System.out.println(i); }

11 FOR-EACH LOOPS… The only catch with for-each loops is that you don’t know where in the array you are… so you can’t do anything special for the first or last element! You also can’t do anything based on the current index (like comparing or changing the next or previous values). This catch makes the for-each loop only useful in certain situations. A regular for loop is much more flexible, and is always okay to use instead.

12 LET’S TRY IT! Write a program (you don’t have to use any methods other than main!) that asks for a certain number of words, then prints the average length and the number of words whose length exceeds the average: Before you start… what approach would you use? How many words? 5 Word #1? ice Word #2? cream Word #3? social Word #4? sprinkles Word #5? chocolate Average word length: 6.4 Number of words longer than average: 2

13 WHAT WE COVERED You should now understand how an array generally works You should be able to construct an array You should be able to initialize an array with a set of values You should be able to iterate over an array