Professional Documents
Culture Documents
1. Arrays in java are objects which belongs to class Array. 2. Array objects implements only static arrays. i.e once you create an array, its length is fixed. User can not change the length after array creation. 3. Java follows strict bound checking for referencing array elements. If an attempt is made to reference the elements outside the bounds then ArrayIndexOutOfBoundsException will be thrown at run time. 4. Array elements can be referenced from 0 as LB to length-1 as UB. 5. Java also follows strict type checking for Array elements. If an attempt is made to store the elements of another type then ArrayStoreException will be thrown at run time. 6. Length is the attribute of each array which can be referenced by <arrayreference> . <length>
Examples
int a[] = { 10,8,6}; for(int i=0;i<a.length;i++) System.out.println(a[i]); 10 8 6
//int table[][]={0,0,0,1,1,1}; //int table[2][3]={0,0,0,1,1,1}; int table[][]={{0,0,0},{1,1,1}}; for(int i=0;i<2;i++) { for(int j=0;j<3;j++){ System.out.print(table[i][j]);} System.out.println();}
000 111
Exercise1: Define a class Student with following attributes 1. name 2. IdNo 3. Marks in three subjects and three tests Provide following operations 1. Accessor methods for all instance fields 2. A suitable constructor 3. Mutator methods for marks in all subjects 4. total marks in each subject as well as grand total Write test class which creates an array of 10 Students and display the name and id of students with highest and lowest total.
Exercise 2: class Matrix { private int ROWS; private int COLS; // Provide Accessor Methods // A constructor with rows and cols // Methods for adding/subtracting/multiplying two matrices // A mutator method for setting elements at given row & index // Filling matrix }
Strings in Java
1. strings in java are handled by two classes String & StringBuffer 2. String -- Immutable class, StringBuffer - Mutable class Creates string from character array from 3. Constructors : index start to start+numChars -1 1. String() 2. String(char chars[]) 3. String(char chars[],int start, int numChars) 4. String(String x) 5. String(byte bytes[]) 6. String(byte bytes[],int start, int numChars)
String Examples 1. Creating string from another string value String name = Pankaj or String name = new String(Pankaj); 2. Creating string from character array char name[] = {P,A,N,K,A,J}; String n = new String(name); String n1 = new String(name,2,2); 3. Creating string from another string reference String n1 = new String(Pankaj); String n2 = new String(n1);
5. Creating a String from byte array byte[] b = { 65,56,66 28, 288,399}; String str1 = new String(b); System.out.println(str1); String str2 = new String(b,4,6); System.out.println(str2); 6. ArrayIndexOutOfBoundsException char[] chr = { o,o,p}; String str1 = new String(chr,2,2);
3. String Conversion : public String toString() 4. character Extraction : (i) char charAt(int where) (ii) void getChars(int sourceStart, int SourceEnd, char target[], int targetStart) (iii) byte[] getBytes() (iv) char[] toCharArray() 5.String Comparison (i) boolean equals(Object src) (ii) boolean equalsIgnorecase(String str) (iii)boolean regionmatches(int startIndex, String str2, int str2Startindex,int numChars) (iv)boolean regionmatches(boolean Ignorecase, int startIndex, String str2, int str2Startindex, int numChars) (v) boolean startsWith(String str) (vi) boolean endsWith(String str) (vii) equals() versus == (vii) int compareTo(String other)