Cs212: Data Structures Computer Science Department Lecture 2: Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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 Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
 2005 Pearson Education, Inc. All rights reserved Arrays.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Arrays Chapter 7.
Array in C# Array in C# RIHS Arshad Khan
Pointers and Dynamic Arrays
Arrays Chapter 7.
Java Array Object Chuen-Liang Chen Department of Computer Science
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
Advanced Programming Chapter 8: Arrays
Arrays ICS 111: Introduction to Computer Science I
Java How to Program, Late Objects Version, 10/e
7 Arrays.
Command Line Arguments
Lecture 18 Arrays and Pointer Arithmetic
Review of Arrays and Pointers
INC 161 , CPE 100 Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays .
Topics discussed in this section:
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
Single-Dimensional Arrays chapter6
7 Arrays.
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
How do you do the following?
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Cs212: Data Structures Computer Science Department Lecture 2: Arrays

Lecture Contents Arrays What is array? Array declaration Array initialization Array of objects Multidimensional Arrays Array Advantages and Disadvantages 10-Jan-19 Computer Science Department

Arrays Definition An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred to as array elements, can be addressed using a number, called index or subscript. Index/subscript starts begins with 0 Array has fixed size and cannot be changed after it has been declared. 10-Jan-19 Computer Science Department

Arrays 10-Jan-19 Computer Science Department

Array Name The name of the array is a pointer to its first element c[ 0 ] -45 6 72 1543 -89 62 -3 1 6453 78 Name of array (note that all elements of this array have the same name, c) c[ 1 ] c[ 2] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] Index (or subscript) of the element in array c c[ 10] 10-Jan-19 Computer Science Department c[ 11 ] Fig. 7.1 A 12-element array.

Index Also called subscript Position number in square brackets Must be positive integer or integer expression a = 5; b = 6; c[ a + b ] += 2; Adds 2 to c[ 11 ] 10-Jan-19 Computer Science Department

Examine array c c is the array name c.length accesses array c’s length c has 12 elements ( c[0], c[1], … c[11] ) The value of c[0] is –45 10-Jan-19 Computer Science Department

Array declaration Array must be declared before it is used. Array occupies a contiguous memory space. The memory spaces an array needs is calculated by multiplying the number of elements by the memory required for their types, example if we have an array of 10 elements, this space is 10*sizeof(float) = 40 bytes 10-Jan-19 Computer Science Department

Array declaration con.. One way to declare and initialize an array is as follows: element_type[] array_name = {init_val_0,init_val_1,…,init_val_N−1}; 10-Jan-19 Computer Science Department

Declaring and Creating arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array We can create arrays of objects too String b[] = new String[ 100 ]; 10-Jan-19 Computer Science Department

Create 10 ints for array; each int is initialized to 0 by default 1 // Fig. 7.2: InitArray.java 2 // Creating an array. 3 import javax.swing.*; 4 5 public class InitArray { 6 7 public static void main( String args[] ) 8 { 9 int array[]; // declare reference to an array 10 11 array = new int[ 10 ]; // create array 12 13 String output = "Index\tValue\n"; 14 15 // append each array element's value to String output 16 for ( int counter = 0; counter < array.length; counter++ ) 17 output += counter + "\t" + array[ counter ] + "\n"; 18 19 JTextArea outputArea = new JTextArea(); 20 outputArea.setText( output ); 21 22 JOptionPane.showMessageDialog( null, outputArea, 23 "Initializing an Array of int Values", 24 JOptionPane.INFORMATION_MESSAGE ); 25 26 System.exit( 0 ); 27 28 } // end main 29 30 } // end class InitArray Create 10 ints for array; each int is initialized to 0 by default Declare array as an array of ints array.length returns length of array array[counter] returns int associated with index in array 10-Jan-19 Computer Science Department

Each int is initialized to 0 by default 10-Jan-19 Computer Science Department

Example /**Adds all the numbers in an integer array. */ public static int sum(int[] a) { int total = 0; for (int i=0; i < a.length; i++) // note the use of the length variable total += a[i]; return total; } 10-Jan-19 Computer Science Department

Arrays are Objects Arrays in Java are special kinds of objects. In fact, this is the reason we can use the new operator to create a new instance of an array. An array can be used just like any general object in Java, but we have a special syntax (using square brackets, "[" and "]") to refer to its members. An array in Java can do everything that a general object can. Since an array is an object, though, the name of an array in Java is actually a reference to the place in memory where the array is stored. 10-Jan-19 Computer Science Department

The fact that arrays in Java are objects has an important implication when it comes to using array names in assignment statements. For when we write something like b = a; in a Java program, we really mean that b and a now both refer to the same array. So, if we then write something like b[3] = 5; 10-Jan-19 Computer Science Department

10-Jan-19 Computer Science Department

Multidimensional Arrays Can be described as "arrays of arrays". Multidimensional arrays are not limited to two indices. 10-Jan-19 Computer Science Department

Memory Representation of 2D Array 10-Jan-19 Computer Science Department

Array Advantages and Disadvantages Easier to declare and use. Can be used with most of the data types. Array Disadvantages: Fixed size data. If not all the array elements are used. waste of memory space. If more array elements are required if the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values. 10-Jan-19 Computer Science Department