Professional Documents
Culture Documents
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
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( )
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); } }
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