0% found this document useful (0 votes)
139 views

Arrays in Java

The document discusses arrays in Java. Some key points covered are: - Arrays in Java are objects that belong to the Array class. Once created, an array's length cannot be changed. - Java uses bound checking for array element access and will throw an exception if an invalid index is used. - Arrays can be one or two dimensional. Syntax is provided for declaring both types. - The Arrays utility class contains methods for sorting, searching, filling and comparing arrays.

Uploaded by

crocoreader
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Arrays in Java

The document discusses arrays in Java. Some key points covered are: - Arrays in Java are objects that belong to the Array class. Once created, an array's length cannot be changed. - Java uses bound checking for array element access and will throw an exception if an invalid index is used. - Arrays can be one or two dimensional. Syntax is provided for declaring both types. - The Arrays utility class contains methods for sorting, searching, filling and comparing arrays.

Uploaded by

crocoreader
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Arrays in Java

Arrays in java are objects which belongs to class Array. 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. 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. Array elements can be referenced from 0 as LB to length-1 as UB. 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. 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 (Two Dimensional Arrays)


1. int[ ][ ] data = new int[3][3];
data is 2-D int an array, row index 0 to 2 , col index 0 to 2

2. float values[][] = new float[10][4];


values is 2-D float array, row index 0 to 9 , col index 0 to 3

3. int table[][] = {{ 0,0,0},{1,1,1}}; // 2 by 3 Array. 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(); }

WRONG
000 111

class arrTest { public static void main(String args[]) { int data[][] ={{0,1},{0,1,2},{1},{1,2,3,4,5}}; System.out.println(data[0][0]); //System.out.println(data[0][2]); System.out.println(data[1][2]); System.out.println(data[2][0]); //System.out.println(data[2][1]); } // End of main() Method } // End of class arrTest

0
ArrayIndexOutofBounds Exception

2 1
ArrayIndexOutofBounds Exception

Array of Objects
BOX box[] = new BOX[6];

BOX[ ]
box[0] box[1] box[2] box[3] box[4] box[5] null null null null null null

box

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.

SELF STUDY

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 cont


static boolean equals(boolean[] a, boolean[] a2)

static boolean
static boolean static boolean static boolean static boolean static boolean static boolean static boolean

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 cont..


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 cont


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 cont..


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)

Array Method Examples


import java.util.*; Import java.util package to use Arrays class class ArrayExample { public static void main(String args[]) { int x[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3

double data[] = { 12.5,34.6,90.56,12.34,12.56}; double array Size = 5 LB=0 UB =4 float values[] = { 10.45f,23.56f,12.67f}; int arrays Size = 3 LB=0 UB =2 double data1[] = new double[10]; double array Size = 10 LB=0 UB =9 boolean flags[] = new boolean[5]; boolean array Size = 5 LB=0 UB =4 int x1[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3

System.out.println(Arrays.binarySearch(x,20));
Arrays.sort(x); for(int i=0;i<x.length;i++) System.out.print(x[i]+" "); System.out.println(); Prints index of 20 in x Sorts elements of x 3

Prints Elements of x 6 8 10 20

Arrays.fill(data1,12.56); for(int i=0;i<data1.length;i++) System.out.print(data1[i]+" "); System.out.println();

Fills a single value 12.56 in all indexes of data1

Prints Elements of data1

12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56

Arrays.fill(flags,2,5,true); //Arrays.fill(flags,2,6,true); for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); System.out.println(); System.out.println(Arrays.equals(x,x1)); Arrays.sort(values); for(int i=0;i<values.length;i++) System.out.print(values[i]+" "); System.out.println();

Fills true value in boolean array flags from index 2 to 4 ArrayIndexOutOfBoundsEx ception false false true true true

Prints true or false whether x and x1 equals false


10.45 12.67 23.56

} } // End of class

OUTPUT

D:\java\bin>java ArrayExample 3 6 8 10 20 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 false false true true true false 10.45 12.67 23.56

Predict OutPut
int x[] = new int[10]; for(int i=0;i<x.length;i++) System.out.print(x[i]+" "); boolean flags[] = new boolean[10]; for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); false false false false false false false false false false boolean flags[] = new boolean[10]; Arrays.fill(flags,2,8,true); for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" "); false false true true true true true true false false 0000000000

boolean flags[] = new boolean[10]; Arrays.fill(flags,2,11,true); for(int i=0;i<flags.length;i++) System.out.print(flags[i]+" ");

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 11 at java.util.Arrays.rangeCheck(Arrays.java:1325) at java.util.Arrays.fill(Arrays.java:2213) at ArrayExample2.main(ArrayExample2.java:7)

Exercise1: class Student { private String name; private String idno; private double marks[ ][ ]; // Assume all accessor and mutator methods defined earlier
// Define a method which returns a Student with highest total in all subjects
Student getStudentWithHighestScore(Student[] students) { . }

// Define a method which returns a Student with highest total in a given subject
Student getStudentwithSubjectHighest(Student[] students , int subject) { . } }

Exercise 2: class Matrix { private int ROWS; private int COLS; double values[][]; Matrix(int r,int c) { . } // Provide Accessor Marks for getting a element, all elements of a a given row, all elements of a given column // Provide Mutator methods for the same Matrix add(Matrix other) { .. } static Matrix add(Matrix first, Matrix second) { } // Supply opeartions for multiplication as well as subtarction // Methods for matrix equallity (static flavor also) // Transpose of a matrix // boolean isTranspose(Matrix other) returns true if other is transpose of this matrix }

You might also like