You are on page 1of 19

Arrays in Java

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>

Arrays in Java continued.


Syntax : One-Dimensional Arrays : type[] arrayname = new type[Size]; or type arrayname[] = new type[size]; Examples : 1. int[] marks = new int[10]; marks is an int array, length = 10 LB index =0 , UB index = 9 2. float[] values = new float[20]; values is an float array, length = 20 LB index =0 , UB index =1 9 3. double cgpa[] = new double[5]; cgpa is double array, length = 5 LB index =0 , UB index = 4 4. box[] b = new box[20]; Array of Objects b is a box array, length = 20 LB index =0 , UB index = 19 5. point points[] = new point[20]; points is a point array, length = 20 LB index =0 , UB index =19

Arrays in Java continued.


Syntax : Two-Dimensional Arrays : type[][] arrayname = new type[Row_Size][Col_Size]; or type arrayname[][] = new type[Row_Size][Col_Size]; Row index varies from 0 to Row_Size 1 Column index varies from 0 to Col_Size 1 Examples : 1. int[][] data = new int[3][3]; data is 2-D int array, length = 9 row index 0 to 2 col index 0 to 2 2. float values[][] = new float[10][4]; values is 2-D float array, length = 40 row index 0 to 9 col index 0 to 3 3. Int table[][] = {{ 0,0,0},{1,1,1}}; // initializes first row to 0 & second to 1 4. Variable Size Array: int x[][] = new int[3][]; x[0] = new int[2]; x[1] = new int[4]; x[2] = new int[3];

Examples
int a[] = { 10,8,6}; for(int i=0;i<a.length;i++) System.out.println(a[i]); 10 8 6

int a[3] = { 10,8,6}; for(int i=0;i<a.length;i++) System.out.println(a[i]);

']' expected int a[3] = { 10,8,6}; ^ 1 error

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

A Brief Look at the Arrays class


1. Arrays class in java.util package is defined as follows public class Arrays extends Object 2. This class contains all the methods required for manipulating arrays such as sorting and seraching 3. The methods in this class all throw a NullPointerException if the specified array reference is null. 4. This class is member of Javas Collection Framework.

Important Methods of Arrays class


static int binarySearch(byte[] a, byte key) static int binarySearch(char[] a, char key) static int binarySearch(double[] a, double key) static int binarySearch(float[] a, float key) static int binarySearch(int[] a, int key) static int binarySearch(long[] a, long key) static int binarySearch(Object[] a, Object key) static int binarySearch(Object[] a, Object key, Comparator c) static int binarySearch(short[] a, short key)

Important Methods of Arrays class


static boolean static boolean static boolean static boolean static boolean static boolean static boolean static boolean static boolean equals(boolean[] a, boolean[] a2) equals(byte[] a, byte[] a2) equals(char[] a, char[] a2) equals(double[] a, double[] a2) equals(float[] a, float[] a2) equals(int[] a, int[] a2) equals(long[] a, long[] a2) equals(Object[] a, Object[] a2) equals(short[] a, short[] a2)

Important Methods of Arrays class


static void static void static void static void static void static void static void static void static void fill(boolean[] a, boolean val) fill(boolean[] a, int fromIndex, int toIndex, boolean val) fill(byte[] a, byte val) fill(byte[] a, int fromIndex, int toIndex, byte val) fill(char[] a, char val) fill(char[] a, int fromIndex, int toIndex, char val) fill(double[] a, double val) fill(double[] a, int fromIndex, int toIndex, double val) fill(float[] a, float val)

Important Methods of Arrays class


static void static void static void static void static void static void static void static void static void fill(float[] a, int fromIndex, int toIndex, float val) fill(int[] a, int val) fill(int[] a, int fromIndex, int toIndex, int val) fill(long[] a, int fromIndex, int toIndex, long val) fill(long[] a, long val) fill(Object[] a, int fromIndex, int toIndex, Object val) fill(Object[] a, Object val) fill(short[] a, int fromIndex, int toIndex, short val) fill(short[] a, short val)

Important Methods of Arrays class


static void static void static void static void static void static void static void static void static void static void sort(byte[] a) sort(byte[] a, int fromIndex, int toIndex) sort(char[] a) sort(char[] a, int fromIndex, int toIndex) sort(double[] a) sort(double[] a, int fromIndex, int toIndex) sort(float[] a) sort(float[] a, int fromIndex, int toIndex) sort(int[] a) sort(int[] a, int fromIndex, int toIndex)

Important Methods of Arrays class


static void static void static void static void static void static void static void static void sort(long[] a) sort(long[] a, int fromIndex, int toIndex) sort(Object[] a) sort(Object[] a, Comparator c) sort(Object[] a, int fromIndex, int toIndex) sort(Object[] a, int fromIndex, int toIndex, Comparator c) sort(short[] a) sort(short[] a, int fromIndex, int toIndex)

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);

String Examples continued.


4.

Creating an Empty String


String str = new String();

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);

IMPORTANT STRING METHODS


1.String Length : int length() ; returns the length of the invoking string Examples : System.out.println(Pankaj Vyas.length()); String city = Pilani Jaipur; System.out.println(city.length()); 2. String concatenation : Use of + operator & concat() method String concat(String str) String name = Pankaj + Vyas; String n1=Pilani; String n2 =Object; String n3 = n1+ + n2; String s1 = one; String s2 = s1.concat(two); OR String s2 = s1 + two; Note : You can concatenate Strings with other data types also. String s = 2+2+Four; String = four:+(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)

You might also like