Array Strings and Vectors INTRODUCTION Array is a

  • Slides: 24
Download presentation
Array, Strings and Vectors

Array, Strings and Vectors

INTRODUCTION Array is a group of continuous or related data items that share a

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

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

ONE-DIMENSIONAL ARRAY A list of items can be given one variable name using only

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 Onedimensional 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

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: 1. Declaring the array 2. Creating memory locations 3. Putting values into the memory locations

DECLARATION OF ARRAYS Arrays in Java may be declared in two forms: Form 1

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

CREATION OF ARRAYS Java allows to create arrays using new operator only , as

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

INITIALIZATION OF ARRAYS In this step values are put into the array created. This

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

Points to ponder: Java creates array starting with a subscript of 0 and ends

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

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 a. Size = a. Length;

TWO-DIMENSIONAL ARRAYS In this, the first index selects the row and the second index

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 my. Array[ ][ ]; my. Array = new int [3] [4];

REPRESENTATION OF TWODIMENSIONAL ARRAY IN MEMORY column 0 [0][0] Row 0 310 column 1

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

VARIABLE SIZE ARRAY Java treats multidimensional arrays as “arrays of arrays”. A two-dimensional array

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

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. Array[ ] = new char[4]; char. Array[0] = ‘J’; char. Array[1] = ‘a’;

 In Java, strings are class objects and implemented using two classes, namely, String

In Java, strings are class objects and implemented using two classes, namely, String and String. Buffer. 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.

DECLARATION OF STRING String string. Name; String. Name = new String (“string”); Example: String

DECLARATION OF STRING String string. Name; String. Name = new String (“string”); Example: String first. Name; first. Name = new String(“Anil”);

STRING ARRAYS Array can be created and used that contain strings. String item. Array[

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

STRING METHODS String class defines a number of methods that allow us to accomplish

STRING METHODS String class defines a number of methods that allow us to accomplish a variety of string manipulation tasks. STRING BUFFER CLASS String. Buffer is a peer class of String. While String creates strings of fixed_length, String. Buffer 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

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 int. Vect = new Vector( );

VECTORS ARE CREATED AS ARRAY AS FOLLOWS: Vector int. Vect = new Vector( ); Vector list = new Vector(3); 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.

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 ,

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 Simple Type Wrapper Class boolean Boolean char Character

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