Array, Strings and Vectors

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

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.
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.
©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.
© 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.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 8 Arrays and Strings
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.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8: Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
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 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.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Arrays Chapter 7.
Two-Dimensional Arrays
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Fifth Lecture Arrays and Lists
A simple way to organize data
JavaScript: Functions.
Arrays, For loop While loop Do while loop
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Java How to Program, Late Objects Version, 10/e
7 Arrays.
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.
EKT150 : Computer Programming
Declaration, assignment & accessing
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Arrays.
7 Arrays.
Arrays in Java.
Arrays Arrays A few types Structures of related data items
C++ Array 1.
Arrays.
Presentation transcript:

Array, Strings and Vectors

INTRODUCTION Array is a group of continuous or related data items that share a common name. Syntax: array_name[value]; Example: salary[10];

CONTENTS OF ARRAY ONE-DIMENSIONAL ARRAY CREATING AN ARRAY DECLARATION OF ARRAY CREATION OF ARRAY INITIALIZATION OF ARRAY ARRAY LENGTH TWO-DIMENSIONAL ARRAY VARIABLE-SIZE ARRAY

ONE-DIMENSIONAL ARRAY A list of items can be given one variable name using only one subscript and such a variable is called a single- subscripted or One- dimensional array. Express as: x[1],x[2]………x[n] The subscript of an array can be integer constants, integer variables like i, or expressions that yield integers.

CREATING AN ARRAY Like any other variables, array must be declared and created in the computer memory before they are used. Creation of an array involves three steps:- Declaring the array Creating memory locations Putting values into the memory locations

DECLARATION OF ARRAYS Arrays in Java may be declared in two forms: Example: int number[ ]; float average[ ]; int[ ] counter; float[ ] marks; type arrayname[ ]; Type[ ] arrayname;

arrayname = new type[size]; CREATION OF ARRAYS Java allows to create arrays using new operator only , as shown below: Examples: number = new int[5]; average = new float[10]; arrayname = new type[size];

INITIALIZATION OF ARRAYS In this step values are put into the array created. This process is known as initialization. Example: number[0]=35; number[1]=40; . . . . . . . . . . . . . number[n]=19; arrayname[subscript] = value;

Points to ponder: Java creates array starting with a subscript of 0 and ends with a value less than the size specified. Trying to access an array beyond its boundaries will generate an error message. Array can also be initialize as the other ordinary variables when they are declared. Array initializer is a list of values separated by commas and surrounded by curly braces.

ARRAY LENGTH All arrays store the allocated size in a variable named length. To access the length of the array a using a.length. Each dimension of the array is indexed from zero to its maximum size minus one. Example: int aSize = a . Length;

TWO-DIMENSIONAL ARRAYS In this, the first index selects the row and the second index selects the column within that row. For creating two-dimensional array, same steps are to be followed as that of simple arrays. Example: int myArray[ ][ ]; myArray = new int [3] [4];

REPRESENTATION OF TWO-DIMENSIONAL ARRAY IN MEMORY column 0 column 1 column 2 [0][0] [0][1] [0][2] Row 0 Row 1 Row 2 310 275 365 210 190 325 405 235 240

VARIABLE SIZE ARRAY Java treats multidimensional arrays as “arrays of arrays”. A two-dimensional array declare as: int x[ ][ ] = new int [3][ ]; x[0] = new int[2]; x[1] = new int[4]; x[2] = new int[3];

STRINGS It is the most common part of many java programs. String represent a sequence of characters. The simplest way to represent a sequence of characters in java is by using a character array. Example: char charArray[ ] = new char[4]; charArray[0] = ‘J’; charArray[1] = ‘a’;

In Java, strings are class objects and implemented using two classes,namely, String and StringBuffer. A java string is an instantiated object of the String class. Java string as compared to C strings, are more reliable and predicable. A java string is not a character array and is not NULL terminated.

StringName = new String (“string”); firstName = new String(“Anil”); DECLARATION OF STRING String stringName; StringName = new String (“string”); Example: String firstName; firstName = new String(“Anil”);

STRING ARRAYS Array can be created and used that contain strings. Above statement create following effects: String itemArray[ ] = new String [3]; An itemArray of size 3 to hold three string constants. Strings can be assign to itemArray element:- •using three different statements • more efficiently using a for loop

STRING METHODS STRING BUFFER CLASS String class defines a number of methods that allow us to accomplish a variety of string manipulation tasks. STRING BUFFER CLASS StringBuffer is a peer class of String. While String creates strings of fixed_length, StringBuffer creates strings of flexible length that can be modified in term of both length and content. We can insert characters and substrings in the middle of a string to the end.

VECTORS In Java the concept of variable arguments to a function is achieved by the use of Vector class contained in the java.utl package. This class can be used to create a generic dynamic array known as vector that can hold objects of any type and any number. The objects in this do not have to be homogenous. Array can be easily implemented as vectors.

VECTORS ARE CREATED AS ARRAY AS FOLLOWS: Vector intVect = new Vector( ); // declaring without size Vector list = new Vector(3); // declaring with size A vector can be declared without specifying any size explicitly. A vector can accommodate an unknown number of items . Even, when a size is specified, this can be overlooked and a different number of items may be put into the vector.

ADVANTAGES OF VECTOR OVER ARRAYS  It is convenient to use vectors to store objects. A vector can be used to store a list of objects that may vary in size. We can add and delete objects from the list as and when required. DISADVANTAGES OF VECTOR OVER ARRAYS We cannot directly store simple data types in a vector. We can only store objects. Therefore , we need to convert Simple types to objects. This can be done using the wrapper classes

WRAPPER CLASSES Since, vectors cannot handle primitive data types like int, float, long ,char, and double. Primitive data types may be converted into object types by using the wrapper classes contained in the java.lang packages. The wrapper classes have a number of unique methods for handling primitive data types and objects.

WRAPPER CLASSES FOR CONVERTING SIMPLE TYPES boolean Boolean char Character double Double float Float int Integer long Long

THANK YOU