You are on page 1of 35

Array, String, Vector, Wrapper, Enum

Array
 Group of contiguous or related data items that share
common name
 Example:
 student[5] – represent the 5th students

 complete set of values are referred to “array”

 individual values are called “element”

 Offers ability to use single name to represent a


collection of items and to refer to an item by specifying
index
One-Dimensional Array
 A list of items can be given one variable name using only one
subscript and such a variable is called single-subscripted
variable or a one-dimensional array.
 Example:
int Number [] =new int[5];
 Values can be assigned
Number[0]
Number[0]=12
Number[1]
Number[1]=23 Number[2]
Number[2]=32 Number[3]
Number[3]=11 Number[4]
Number[4]=22
One-Dimensional Array
 A list of items can be given one variable name using only one
subscript and such a variable is called single-subscripted
variable or a one-dimensional array.
 Example:
int Number [] =new int[5];
 Values can be assigned
Number[0]=12
Number[0] 12
Number[1]=23
Number[2]=32 Number[1] 23
Number[3]=11 Number[2] 32
Number[4]=22 11
Number[3]
Number[4] 22

 We can use elements just like other variables in java


 Number[0]=Number[0]*2;
Creating an Array
 Like any other variable arrays 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 location

 Putting values into the memory locations


Declaration of Arrays
 Arrays in Java may be declared in two forms

type arrayname [ ];
type [ ] arrayname;
 Examples:

 int number[ ];

 float average[ ];

 int [ ] count;

 We do not enter the size of the arrays in the declaration.


Creation of Arrays
 After declaration of array, we need to create it in the

memory.

 Java allows us to create arrays using new operator only as:

 Examples: arrayname = new type [size];

 number=new int[5];

 average=new float[10];
Creation of Arrays
 We can combine first two steps as:

int number[]=new int[5];

Statements Result
number

int number [ ];
points
nowhere
number = new int [ 2 ];

points to
number[0] int object
number[1]
Initialization of Arrays
 The final step is to put values into the array created.

arrayname [ subscript ] = value ;


 Examples:

Number[0]=12
Number[1]=23

 Subscript or index always starts with 0 and ends with a value one

less than the size specified.

 Unlike C, Java protects arrays from overrun and underrun


Initialization of Arrays
 Another way to initialize the array is:

 Examples: type arrayname [ ] = {list of values} ;


int number [ ] = {12,34,45,43};
 Note that no size is given. The compiler allocates enough space for all
the elements specified in the list.
 Unlike C, Java protects arrays from overrun and underruns

 It is possible to assign an array object to another

 int a[ ] = {1,2,3,4,5};
 int b[ ];
 b = a;
Array Length
 In Java, all arrays store the allocated size in a variable named

length
 Example:

 int a [ ] = { 1, 2, 3, 4, 5 };

 int size = a.length;


Two – Dimensional Arrays
 The two dimensional arrays are the arrays in
which elements are stored in rows as well as
columns [Matrix]

 We represent a particular value in matrix by


using two subscripts such as Vij, where V

denotes entire matrix and Vij refers to the


value of ith row and jth column

 Example : v [ 2 ] [ 1 ]

 Here the first index selects the row and the


second index select the column
Creating Two – Dimensional Arrays
 We must follow the same steps as that of
simple arrays.
int v [ ] [ ];
v = new int [ 3 ] [ 4 ];
or
int v [ ] [ ] = new int [ 3 ] [ 4 ];

 This creates a table that can

store 12 integer values, four


across, three down
Initialization Two – Dimensional Arrays
 2D arrays may be initialized by following ways:
Variable Size Arrays
 Java treats multidimensional arrays as “ arrays of array”.

 It is possible to declare a two-dimensional arrays as:


Variable Size Arrays
 Java treats multidimensional arrays as “ arrays of array”.

 It is possible to declare a two-dimensional arrays as:


Strings
 String represents a sequence of characters
 The easiest way to represent the sequence of character is by
using character array
 char str[ ] = { „a‟ , „b‟, „c‟ };
 Difficulty in copying one array in other and other
functionality
 In Java, Strings are class objects and implemented using two
classes:
 String
 StringBuffer
String
 A Java string is instantiated object of the String class
 Unlike C, Java Strings are more reliable and predictable
 A Java string is character array and is not NULL
terminated
 Strings may be declared as:

String stringname;
stringname = new String (“Hello”);

String stringname = new String (“Hello”);


String Methods
 A Java string can be concatnated using + operator
String name = “Mr” + “Bean”;
 length ( ) method of String class used to get the length of
string
String stringname = new String (“Hello”);
int length = stringname.length()

 String arrays

String stringname [ ] = new String [3];


String Methods
S2=s1.toLowerCase() Converts to lower case

S2=s1.toUpperCase() Converts to uppercase

S2=s1.replace(„a‟,‟b‟) Replace a with b


Remove all the white space beginning and at
S2=s1.trim();
end of string
Compare two string and returns true if same
S1.equals(s2)
otherwise false
S1.equalsIgnoreCase(s2 Compare two string (by ignoring the case)
) and returns true if same otherwise false
s1.charAt(n) Returns the character at n index of string

S1.concat(s2) Concat s1 with s2


StringBuffer Class
 StringBuffer class is peer class of String.
 While String creates strings of fixed length, StringBuffer
class creates strings of flexible length that can be modified in
terms of both length and content
 We can insert characters and substrings in the middle of a
string, or append another string to the end.
StringBuffer str = new StringBuffer (“Hello
SYCO”);
System.out.println(str); =>Hello SYCO
str.setCharAt(8,‟-‟);
System.out.println(str); =>Hello SY-CO
StringBuffer Class Methods

s1.setCharAt(n,‟X‟) Modifies the nth character to X

S1.append(s2) Appends the string s2 to s1 at the end

Insert the string s2 at the position n of the


S1.insert (n,s2)
string s1

Set the length of the string s1 to n


S1.setLength(n) If n < s1.length() s1 is truncated
If n > s1.length() zeros will be added to s1
Vector Class
 Vector class is present in java.util 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 object do not have to be homogeneous.
 Vectors are treated like array as:
Vector v = new Vector ( ); // declaring without size
Vector v = new Vector ( 3 ); // declaring with size
 Note that Vector without size can accommodate an unknown
number of items.
 Even, when size is specified, this can be overlooked
Vector Class Vs Array
Advantages
 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
 We cannot directly store simple data type in vectors, we can
only store objects
 We need to convert simple types to objects using wrapper
classes
Vector Class Methods
List.addElement(item) Add the item specified to the list at the end

List.elementAt(n) Gives the name of nth object


List.size() Gives the number of objects present

List. removeElement(item) Removes the specified item from the list

List. removeElementAt(n) Removes the specified nth item from the list

List.removeAllElements() Removes all items from the list

List.copyInto(array) Copies all items from list to array


Add the item specified to the list at the nth
List.insertElementAt(item,n)
index
Wrapper Class
 Primitive data types may be converted into object types by
using the wrapper classes contained in java.lang package.

 Wrapper classes have a number of unique methods for


handling primitive data Note that Vector without size can
accommodate an unknown number of items.
Converting Primitive Numbers to Object
Number Using Constructor method
Converting Object Numbers to
Primitive Numbers Using
typeValue() method
Converting Numbers to String Using
toString() method
Converting String Objects to Numeric
Objects Using static method valueOf()
Converting Numeric Strings to Primitive
Numbers Using Parsing Method
Autoboxing and Unboxing
 Added in J2SE5.0  Example:
Double d=98.42;
 We can convert primitive data
double db=d.doubleValue();
types to wrapper class types
automatically. using autoboxing and
unboxing
 The compiler generates a code
implicitly to convert primitive Double d=98.42;
type to the corresponding double db=d;
wrapper class type and vice-versa
Enumerated Types
 Added in J2SE5.0 public class Days
{
 enum keyword can be used to as
public static final int Sunday=0;
static final constant public static final int Monday=1;
 Using enum the code can be public static final int Tueday=2;
public static final int Wednesday=0;
rewritten as,
public static final int Thursday=0;
public enum Day public static final int Friday=0;
{SUNDAY,MONDAY,TUESDAY,WEDENS public static final int Saturday=0;
DAY,THURSDAY,FRIDAY,SATURDAY}
}
Advantages of Enumerated Types
 Compile-time type safety

 We can use the enum keyword in switch statement


References
 Programming with Java A Primer – E. Balaguruswamy

You might also like