More Arrays Length, constants, and arrays of arrays By Greg Butler.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Arrays.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
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.
© 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.
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.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Chapter 9 Introduction to Arrays
VB .NET Programming Fundamentals
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
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.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
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.
 Pearson Education, Inc. All rights reserved Arrays.
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.
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.
Java SE 8 for Programmers, Third Edition
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SEEM Java – Basic Introduction, Classes and Objects.
CSE 1201 Object Oriented Programming ArrayList 1.
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 Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
 2005 Pearson Education, Inc. All rights reserved Arrays.
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.
Arrays Chapter 7.
Topic: Classes and Objects
Java How to Program, Late Objects Version, 10/e
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.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Presentation transcript:

More Arrays Length, constants, and arrays of arrays By Greg Butler

Overview/Purpose In the first lesson we learned that an array is a variable that can hold multiple values of the same data type. We also explored the definition, creation and initialization of a single dimensional array to hold primitive data types. In so doing we learned how to declare the array variable and using either the new operator or a list of values. We then looked at a simple program that allows us to retrieve and use the values stored in the array’s elements. This lesson introduces us to the use the length variable to retrieve the size of an array and use a constant with the new operator to specify array size. We then look into the use of “multiple dimensional” arrays. The next lesson will then look into sorting and searching arrays.

Objectives The student when presented with a short answer question, matching question, or programming problem, must demonstrate: –An understanding of the meaning and use of the “length” instance variable to find the size of an array. –The use of the “final” keyword to create a named constant. –An understanding of the meaning and use of constants to specify array size and to make software changes easier to make. –The ability to describe, create, and populate an array of arrays using both the “new” operator and by populating the elements at the time the array is defined. –The ability to access the elements of a multi-dimensional arrays so that they can be update and used in a program.

Finding the Length of an Array Array are actually objects of a particular class. We will explore this in detail later, but for now, it is sufficient to know that the length of an array is stored inside a variable* called length. Usage: my_array_name. length Specify the name of your array followed by the dot operator (“.”) and the term “length” and it retrieves the size of the named array. In this next example we modify our bowlingScores program to use the array length method to determine the maximum value in the for loop, instead of specifying the maximum value explicitly. * In OO terms, it is an instance variable, because every array you create has its own value for “length”.

Finding the Length of an Array Example class ArrayBasicsFindLength{ public static void main(String args[]){ int bowlingScores[] = {117, 127, 110, 85, 135}; int index = 0; // declare and initialize a variable to hold the // current array index number for (index = 0; index < bowlingScores.length; index++) { // display the results System.out.println (“ Element “+ index + “ added to 5 is: “ + outputValue); } // end for } //end main } //end class ArrayBasicsOne Since the array is of size 5, bowlingScores.length is 5.

Using Constants (final variables) to Specify Array Size Constants are variables whose value cannot be changed by the program once it is assigned. “final” is the reserve word that designates a constant. By convention, constant names appear in upper case. The use of constants helps make a program more maintainable; you only need to make the change one place in the program (where the value is initially assigned to a constant) and it is applied everyplace the constant appears In our example, we create and initialize an array and then read out the elements using a constant to designate the size of the array.

import javax.swing.*; public class ArrayBasicsConstants{ public static void main(String args[]){ final int CONST_DEMO_SIZE = 6; int arrayConstants[ ] = new int [CONST_DEMO_SIZE ]; int index = CONST_DEMO_SIZE -1; // load the array highest to lowest element do { arrayConstants [index] = Integer.parseInt (JOptionPane. showInputDialog( "What do you want in the next array element?")); --index; } while (index >=0); // end do-while // read out array contents lowest to highest element for (int index2 = 0; index2 < CONST_DEMO_SIZE; ++index2) { System.out.println ("point 2: " + index2); // display the results System.out.println (" Element "+ index2 + " is: " + arrayConstants [index2]) ; } // end for } //end main } //end class ArrayBasicsOne Using Constants to Specify Array Size- Example Create a constant, set it as 6 Create an array, using the constant to specify the array size We can change the array size by simply changing the 6 to the size we want, an the program will continue to work correctly. If we had not used a constant, how many changes would we need to make?

Multi-Dimensional Arrays: Arrays of Arrays You may find that you need an array that has more than one dimension. Example: I need to record the temperature every 6 hours for 4 days. A structure that supports this might look like: Day Java does NOT support multi-dimensional arrays, per se, but you can create and array of arrays The general form for a two-dimensional integer array of arrays, like above is: int arrayName [ ] [ ] = new int [no_rows] [no_cols]; In our example we create the above array, have the user populate it, and then display the result on the console You must set the size of the arrays starting left … … to right. If you use values, rather than the “new” operator when creating the array, you must populate the arrays left to right

Multi-Dimensional Arrays: Arrays of Arrays Example import javax.swing.*; public class ArrayBasicsMultiDim{ public static void main(String args[]){ final int CONST_NO_DAYS = 4; final int CONST_NO_MEAS = 4; int arrayDailyTemps[ ][ ] = new int [CONST_NO_DAYS][CONST_NO_MEAS]; for (int indexDay = 1; indexDay <= CONST_NO_DAYS; ++indexDay) { for (int indexMeas = 1; indexMeas <= CONST_NO_MEAS; ++indexMeas) arrayDailyTemps [indexDay-1][indexMeas-1] = Integer.parseInt ( JOptionPane. showInputDialog( "What do you want in day " + indexDay + " measure " + indexMeas +" ?")); }// end outer for for (int indexDay = 1; indexDay <= CONST_NO_DAYS; ++indexDay) { for (int indexMeas = 1; indexMeas <= CONST_NO_MEAS; ++indexMeas) System.out.println("Day " +indexDay + " measure " + indexMeas + " is: " + arrayDailyTemps[indexDay-1][indexMeas-1]); }// end outer for // System.exit(0); was left out to freeze the console so that we can // see the consol output } //end main } //end class ArrayBasicsMultiDim Create constants to set the maximum number of rows (days) and columns (measures) Create the array Load each array element by addressing it using the form arrayName[row_index][col_index]

Summary Associated with each array is a variable named “length” that contains the length (a.k.a. size) of the array We can specify named constants, using the reserve word “final.” Once assigned a value, they cannot be changed by the program. They make software maintenance easier by allowing us to change a value in one place, rather than having to search through the code. Normally, the name appears in uppercase. Java doesn’t have multi-dimensional arrays, but we can use an array of arrays to meet this need. We must set the element sizes from left to right.