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.

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

Introduction to Java 2 Programming Lecture 5 Array and Collections.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©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 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
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.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
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.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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 
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.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
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.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
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.
Array and String.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Computer Programming BCT 1113
Array, Strings and Vectors
Fifth Lecture Arrays and Lists
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Java Arrays & Strings.
Can store many of the same kind of data together
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Arrays in Java.
Arrays Arrays A few types Structures of related data items
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

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 is called a index or superscript. Base index = 0 * The ability to use a single name to represent a collection of items and refer to an item by specifying the item number enables us to develop concise and efficient programs. For example, a loop with index as the control variable can be used to read the entire array, perform calculations, and print out the results. 2 Lecture # 4

3 * Arr[7]={69,61,70,89,23,10,9} index values Lecture # 4

* Like any other variables, arrays must declared and created before they can be used. Creation of arrays involve three steps: * Declare the array * Create storage area in primary memory. * Put values into the array (i.e., Memory location) * Declaration of Arrays: * Form 1: Type arrayname[] * Form 2: * Type [] arrayname; * Examples: int[] students; int students[]; * Note: we don’t specify the size of arrays in the declaration. 4 Lecture # 4

* After declaring arrays, we need to allocate memory for storage array items. * In Java, this is carried out by using “new” operator, as follows: * Arrayname = new type[size]; * Examples: * students = new int[7]; 5 Lecture # 4

* Once arrays are created, they need to be initialised with some values before access their content. A general form of initialisation is: * Arrayname [index/subscript] = value; * Example: * students[0] = 50; * students[1] = 40; * Like C, Java creates arrays starting with subscript 0 and ends with value one less than the size specified. 6 Lecture # 4

* Arrays are fixed length * Length is specified at create time * In java, all arrays store the allocated size in a variable named “length”. * We can access the length of arrays as arrayName.length: e.g. int x = students.length; // x = 7 * Accessed using the index e.g. int x = students [1]; // x = 40 7 Lecture # 4

// StudentArray.java: store integers in arrays and access public class StudentArray{ public static void main(String[] args) { int[] students; students = new int[7]; System.out.println("Array Length = " + students.length); for ( int i=0; i < students.length; i++) students[i] = 2*i; System.out.println("Values Stored in Array:"); for ( int i=0; i < students.length; i++) System.out.println(students[i]); } :w 8

* Arrays can also be initialised like standard variables at the time of their declaration. * Type arrayname[] = {list of values}; * Example: int[] students = {55, 69, 70, 30, 80}; * Creates and initializes the array of integers of length 5. * In this case it is not necessary to use the new operator. 9 Lecture # 4

// StudentArray.java: store integers in arrays and access public class StudentArray{ public static void main(String[] args) { int[] students = {55, 69, 70, 30, 80}; System.out.println("Array Length = " + students.length); System.out.println("Values Stored in Array:"); for ( int i=0; i < students.length; i++) System.out.println(students[i]); } 10

* Two dimensional arrays allows us to store data that are recorded in table. For example: * Table contains 12 items, we can think of this as a matrix consisting of 4 rows and 3 columns. Item1Item2Item3 Salesgirl # Salesgirl # Salesgirl # Salesgirl # Sold Person

* Declaration: * int myArray [][]; * Creation: * myArray = new int[4][3]; // OR * int myArray [][] = new int[4][3]; * Initialisation: * Single Value; * myArray[0][0] = 10; * Multiple values: * int tableA[2][3] = {{10, 15, 30}, {14, 30, 33}}; * int tableA[][] = {{10, 15, 30}, {14, 30, 33}}; 12 Lecture # 4

* Java treats multidimensional arrays as “arrays of arrays”. It is possible to declare a 2D arrays as follows: * int a[][] = new int [3][]; * a[0]= new int [3]; * a[1]= new int [2]; * a[2]= new int [4]; 13 Lecture # 4

14

* Strings is set of characters. The easiest way to represent a String (a sequence of characters) is by using an array of characters. * Example: * char place[] = new char[4]; * place[0] = ‘J’; * place[1] = ‘a’; * place[2] = ‘v’; * place[3] = ‘a’; * Although character arrays have the advantage of being able to query their length, they themselves are too primitive and don’t support a range of common string operations. For example, copying a string, searching for specific pattern etc. 15 Lecture # 4

* Declaration and Creation: * String stringName; * stringName = new String (“string value”); * Example: * String city; * city = new String (“Kabul”); * Length of string can be accessed by invoking length() method defined in String class: * int len = city.length(); 16 Lecture # 4

* Java Strings can be concatenated using the + operator. String city = “New” + “York”; String city1 = “Delhi”; String city2 = “New “+city1; * Strings Arrays String city[] = new String[5]; city[0] = new String(“Melbourne”); city[1] = new String(“Sydney”); String megacities[] = {“Brisbane”, “Sydney”, “Melbourne”, “Adelaide”, “Perth”}; 17 Lecture # 4

18 // StringDemo.java: some operations on strings class StringDemo { public static void main(String[] args) { String s = "Have a nice Day"; // String Length = 15 System.out.println("String Length = " + s.length() ); // Modified String = Have a Good Day System.out.println("Modified String = " + s.replace('n', 'N')); // Converted to Uppercse = HAVE A NICE DAY" System.out.println("Converted to Uppercase = " + s.toUpperCase()); // Converted to Lowercase = have a nice day" System.out.println("Converted to Lowercase = " + s.toLowerCase()); } Lecture # 4

19 Lecture # 4