Lecture 6

1,184 views

Published on

Notes on core java

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,184
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 6

  1. 1. Core Java Debasish Pratihari Array :  In Java arrays are objects that represent a group of contiguous memory locations of the same type and referred to by the same name.  The element type for an array can be any primitive data type or object.  All Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.  Declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array  Array variable declarations must indicate a dimension by using []  All arrays are zero-based  Arrays must be indexed by int values or byte, short or char values (as these can be promoted to int)  Using a long index value to access an array causes a compile error  Attempting to access an array with an index less than 0 or greater than the length of the array causes an ArrayIndexOutOfBoundsException to be thrown at runtime  Since arrays are Objects they can be initialized using the new operator  When created, arrays are automatically initialized with the default value of their type  Array references declared as members are initialized to null BUT array references declared in methods are not initialized Lecture/core/array/06 Page #1 feel the Technology…
  2. 2. Core Java Debasish Pratihari Examples of valid array declarations :  String[]s;  String s[];  String[][]s;  String s[][];  String[] s[]; Initializing Array :            String[ ] s = new String[100]; // default values: null boolean[] b = new boolean[4]; // default values: false int i[ ]= new int[5]; int[][] i = new int[10][10]; // default values: 0 String[] oneDimArray = { "abc","def","xyz" }; int[] array = null; int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; int[] a = { 1, primes[2], c, (int)Math.pow(2,2) }; int[] arr = new int[] {1,2,3}; int[][] myArray = new int[5][]; int arrSize = 100; String[] myArray = new String[arrSize]; Illegal Initialization of Array :     String[5] s; int[] array = new int[]; int[][] myArray = new int[][5]; String[] s; s = { "Lakshya", "debasish", "java"}; Lecture/core/array/06 Page #2 feel the Technology…
  3. 3. Core Java Debasish Pratihari Finding length of an Array :  Arrays have a length data field (read only), specifying the number of elements in the array.  The length data field can be accessed through the dot operator.  The bounds of an array are integers between 0 and length – 1  Example for ( int k=0; k < a.length ; k++ ) System.out.println( a[ k ] ); Multidimensional Arrays:  Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays are declared by appending the appropriate number of bracket pairs after the array name.  The number of elements in each direction need not be specified in the declaration.  The number of elements in each direction need not be equal. Example of irregular Array : int[][] irregular = { { 1 }, { 2, 3 }, { 4, 5, 6, 7 }, {0} }; Lecture/core/array/06 Page #3 feel the Technology…
  4. 4. Core Java Debasish Pratihari An Example -Triangle Array : Output int[][] triangle = new int[10][]; for ( int j = 0; j<triangle.length ; j++ ) triangle[ j ] = new int[ j + 1 ]; for ( int i = 0 ; i < triangle.length ; i++ ) { for ( int j = 0 ; j < triangle[i].length ; j++ ) System.out.print( triangle[i][j] + " " ); System.out.println(); } 0 00 000 0000 00000 000000 0000000 00000000 000000000 0000000000 Coping Array : 25%    Arrays can be copied using the Java System method arraycopy(): Signature:public static native void arraycopy(Object src, int src_position, Object dst, int dst_position, int length) Parameters: src - the source array. src_position - start position (first cell to copy) in the source array. dst - the destination array. dst_position - start position in the destination array. length - the number of array elements to be copied.  By default, all Java arrays support the clone method. Things to Remember :  An array is a container object that holds a fixed number of values of a single type.  The length of an array is established when the array is created. After creation, its length is fixed.  Each item in an array is called an element  each element is accessed by its numerical index. Lecture/core/array/06 Page #4 feel the Technology…

×