Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Based on slides from Deitel & Associates, Inc.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
Chapter 9: Arrays and Strings
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 Pearson Education, Inc. All rights reserved Arrays.
VB .NET Programming Fundamentals
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Arrays and Strings
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 9 Multidimensional Arrays and the ArrayList Class.
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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays.
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. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Module 1: Array ITEI222 - Advance Programming Language.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Two-Dimensional Arrays
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
JavaScript: Functions.
Java How to Program, Late Objects Version, 10/e
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
JavaScript Arrays.
MSIS 655 Advanced Business Applications Programming
Arrays.
Arrays Chapter 7.
Arrays Arrays A few types Structures of related data items
C++ Array 1.
Arrays.
Presentation transcript:

Arrays

Topics Arrays Declaring and Creating Arrays Examples using Arrays Enhanced for Statement Passing Array Methods Multi Dimensional Array

Arrays Array Refer to a particular element in an array Group of variables (called elements) containing values of the same type. Arrays are objects so they are reference types. Elements can be either primitive or reference types. Refer to a particular element in an array Use the element’s index. Array-access expression—the name of the array followed by the index of the particular element in square brackets, []. The first element in every array has index zero. The highest index in an array is one less than the number of elements in the array. Array names follow the same conventions as other variable names.

Arrays (Cont.) An index must be a nonnegative integer. Can use an expression as an index. An indexed array name is an array-access expression. Can be used on the left side of an assignment to place a new value into an array element. Every array object knows its own length and stores it in a length instance variable. length cannot be changed because it’s a final variable.

Declaring and Creating Arrays Array objects Created with keyword new. You specify the element type and the number of elements in an array-creation expression, which returns a reference that can be stored in an array variable. Declaration and array-creation expression for an array of 12 int elements int[] c = new int[ 12 ]; Can be performed in two steps as follows: int[] c; // declare the array variable c = new int[ 12 ]; // creates the array

Declaring and Creating Arrays (Cont.) In a declaration, square brackets following a type indicate that a variable will refer to an array (i.e., store an array reference). When an array is created, each element of the array receives a default value Zero for the numeric primitive-type elements, false for boolean elements and null for references.

Examples Using Arrays (Cont.) Array initializer A comma-separated list of expressions (called an initializer list) enclosed in braces. Used to create an array and initialize its elements. Array length is determined by the number of elements in the initializer list. int[] n = { 10, 20, 30, 40, 50 }; Creates a five-element array with index values 0–4. Compiler counts the number of initializers in the list to determine the size of the array Sets up the appropriate new operation “behind the scenes.”

Enhanced for Statement Iterates through the elements of an array without using a counter. Avoids the possibility of “stepping outside” the array. Syntax: for ( parameter : arrayName ) statement where parameter has a type and an identifier and arrayName is the array through which to iterate. Parameter type must be consistent with the array’s element type. The enhanced for statement simplifies the code for iterating through an array.

Enhanced for Statement (Cont.) The enhanced for statement can be used only to obtain array elements It cannot be used to modify elements. To modify elements, use the traditional counter-controlled for statement. Can be used in place of the counter-controlled for statement if you don’t need to access the index of the element.

Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without any brackets. Since every array object “knows” its own length, we need not pass the array length as an additional argument. To receive an array, the method’s parameter list must specify an array parameter.

Multidimensional Arrays Two-dimensional arrays are often used to represent tables of values consisting of information arranged in rows and columns. Identify a particular table element with two indices. By convention, the first identifies the element’s row and the second its column. Multidimensional arrays can have more than two dimensions. Java does not support multidimensional arrays directly Allows you to specify one-dimensional arrays whose elements are also one-dimensional arrays, thus achieving the same effect. In general, an array with m rows and n columns is called an m-by-n array.

Multidimensional Arrays (Cont.) Multidimensional arrays can be initialized with array initializers in declarations. A two-dimensional array b with two rows and two columns could be declared and initialized with nested array initializers as follows: int[][] b = { { 1, 2 }, { 3, 4 } }; The initial values are grouped by row in braces. The number of nested array initializers (represented by sets of braces within the outer braces) determines the number of rows. The number of initializer values in the nested array initializer for a row determines the number of columns in that row. Rows can have different lengths.

Multidimensional Arrays (Cont.) The lengths of the rows in a two-dimensional array are not required to be the same: int[][] b = { { 1, 2 }, { 3, 4, 5 } }; Each element of b is a reference to a one-dimensional array of int variables. The int array for row 0 is a one-dimensional array with two elements (1 and 2). The int array for row 1 is a one-dimensional array with three elements (3, 4 and 5).

Multidimensional Arrays (Cont.) A multidimensional array with the same number of columns in every row can be created with an array-creation expression. int[][] b = new int[ 3 ][ 4 ]; 3 rows and 4 columns. The elements of a multidimensional array are initialized when the array object is created. A multidimensional array in which each row has a different number of columns can be created as follows: int[][] b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1 Creates a two-dimensional array with two rows. Row 0 has five columns, and row 1 has three columns.

?