1 / 20

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.

Download Presentation

Arrays in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Arrays in Javacontinued…. 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]; 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 Array of Objects

  3. Arrays in Javacontinued…. 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

  4. 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];

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

  6. A Brief Look at the Arrays class SELF STUDY • Arrays class in java.util package is defined as follows • public class Arrays extends Object • This class contains all the methods required for manipulating arrays such as sorting and seraching • The methods in this class all throw a NullPointerException if the specified array reference is null. • This class is member of Java’s Collection Framework.

  7. Important Methods of Arrays class

  8. Important Methods of Arrays class cont…

  9. Important Methods of Arrays class cont..

  10. Important Methods of Arrays class cont…

  11. Important Methods of Arrays class cont..

  12. Important Methods of Arrays class

  13. Array Method Examples Import java.util package to use Arrays class import java.util.*; class ArrayExample { public static void main(String args[]) { int x[] = {10,6,8,20}; double data[] = { 12.5,34.6,90.56,12.34,12.56}; float values[] = { 10.45f,23.56f,12.67f}; double data1[] = new double[10]; boolean flags[] = new boolean[5]; int x1[] = {10,6,8,20}; int array Size = 4 LB=0 UB =3 double array Size = 5 LB=0 UB =4 int arrays Size = 3 LB=0 UB =2 double array Size = 10 LB=0 UB =9 boolean array Size = 5 LB=0 UB =4 int array Size = 4 LB=0 UB =3

  14. 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 3 Sorts elements of x Prints Elements of x 6 8 10 20 Fills a single value 12.56 in all indexes of data1 Arrays.fill(data1,12.56); for(int i=0;i<data1.length;i++) System.out.print(data1[i]+" "); System.out.println(); Prints Elements of data1 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56 12.56

  15. Fills true value in boolean array flags from index 2 to 4 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(); ArrayIndexOutOfBoundsException false false true true true Prints true or false whether x and x1 equals false 10.45 12.67 23.56 } } // End of class

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

  17. Predict OutPut int x[] = new int[10]; for(int i=0;i<x.length;i++) System.out.print(x[i]+" "); 0 0 0 0 0 0 0 0 0 0 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

  18. 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)

  19. 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) { …………. } }

  20. 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 }

More Related