You are on page 1of 20

Arrays

Creation of an Array involves 3 steps: Declare the array Create memory locations Put values into the memory locations

Arrays
Arrays can be declared in two forms: 1. Type arrayname[ ]; 2. Type[ ] arrayname; Examples: 1. int number[ ]; 2. float average[ ]; 3. int[ ] coutner; 4. float[ ] marks; * We do not enter the size of the array in the declaration.

Creation of Arrays
Arrayname= new type[size]; Number =new int[5]; Average=new float[10];
It is also possible to combine these two steps: int number[ ]=new int[5];

Initialization of Arrays
arrayname[subscript]=value; Example: Number[0]=35;
Or you can use loops to initialize an array

Two-Dimensional Arrays
int myArray[ ] [ ]; myArray=new int[3][4];
Or

int myArray[ ][ ]=new int[3][4]; Initializing 2-D arrays int table[2][3]={ {0,0,0}, {1,1,1} }; Or through loops

Strings & StringBuffer


In Java, Strings are class objects and implemented using two classes, namely, String and StringBuffer. A java is not a character array and is not NULL terminated. String stringName; StringName=new String(string); Or String firstName=new String(Jaypee); To get the length of the string: int n=firstName.length( ); Concatenation using +

String Methods
s2=s1.toLowerCase( ); s2=s1.toUpperCase( ); s1.equals(s2) s1.equalsIgnoreCase(s2) s1.length( ); s1.concat(s2) s1.substring(n) s1.substring(n,m) p.toString( )// Creates a string representation of object p String. valueOf(variable) // Converts parameter value to string representation

StringBuffer Class
It is a peer class of String. String creates strings of fixed length. StringBuffer creates strings of flexible length that can be modified in terms of both length and content. StringBuffer str=new StringBuffer(object language); s1.append(s2); s1.insert(n,s2) // inserts string s2 at position n of s1 s1.setLength(n)

Vectors
Vector is a generic dynamic array that can hold objects of any type and any number The objects do not have to be homogenous. Arrays can be easily implemented as vectors Vector intVect=new Vector( ); Vector list=new Vector(3); Advantages over Array: It is dynamic Disadvantages over Array: Cannot hold primitive data type (int, float etc)

Vector Methods
List.addElement(item) List.elementAt(10); List.size( ) List.removeElement(item) List.removeElementAt(n) List.removeAllElements( )

Program With Vectors

class MyVector{
public static void main(String args[]) { java.util.Vector v=new java.util.Vector(); v.addElement("Neha"); v.addElement("Mragendra"); v.addElement("Gaurav"); System.out.println(v); v.removeElementAt(2); System.out.println(v); } }

Program using Import



import java.util.Vector; class MyVector{


public static void main(String args[]) {

Vector v=new Vector(); v.addElement("Neha"); v.addElement("Mragendra"); v.addElement("Gaurav"); System.out.println(v); v.removeElementAt(2); System.out.println(v);


} }

Wrapper Classes
You know Vectors cannot handle primitive data types like int , float, long ,char and double. So, what should we do?

Wrapper Classes
Ans: convert primitive types to object types How?

Wrapper classes
Ans: Through the use of Wrapper classes Simple Type Wrapper Class boolean Boolean char Character double Double float Float int Integer long Long

Converting Primitive Numbers to Object Numbers


Integer IntVal=new Integer(i); Float FloatVal=new Float(f); Double DoubleVal=new Double(d); Long LongVal=new Long(l);

Converting Object Numbers to Primitive Numbers using typeValue() method


int i=IntVal.intValue( ); float f=FloatVal.floatValue( ); long l=LongVal.longValue( ); double d=DoubleVal.doublevalue( );

Converting Numbers to Strings using toString( ) Method


str=Integer.toString(i) str=Float.toString(f); str=Double.toString(d); str=Long.toString(l);

int i=Integer.parseInt(str); long l=Long.parseLong(str);

Converting Numeric Strings to Primitive Numbers Using Parsing Method

Converting String objects to Numeric objects using the ValueOf()


DoubleVal=Double.ValueOf(str); FloatVal=Float.ValueOf(str); IntVal=Integer.ValueOf(str); LongVal=Long.ValueOf(str);

You might also like