120 likes | 312 Views
Java Arrays. By Srinivas Reddy.S. www.java9s.com. Arrays. Collection of similar data types Stages Declaration Construction Initialization. www.java9s.com. Declaration. Declaring Arrays: int[] marks; byte[] age; Less readable: int marks[]; byte age[];. www.java9s.com.
E N D
Java Arrays By Srinivas Reddy.S www.java9s.com
Arrays • Collection of similar data types Stages • Declaration • Construction • Initialization www.java9s.com
Declaration • Declaring Arrays: int[] marks; byte[] age; Less readable: int marks[]; byte age[]; www.java9s.com
Construction int[] marks; marks = new int[5]; “The size of the array is mandatory” In single line: int[] marks = new int[5]; marks www.java9s.com
Initialization • Initialization is loading the array with the values. int[] marks = new int[5]; marks[0] =20; marks[1]=49; marks[2] =30; marks[3] = 67; marks[4] = 35; 20 49 30 67 35 0 1 2 3 4 www.java9s.com
Initialization – Objects class Pen{ } Pen[] pens = new Pen[3]; pens[0] = new Pen(); Pens[1] = new Pen(); Pens[2] = new Pen(); 0 1 2 www.java9s.com
Array Initializer. • int[] marks ={20,39,40,30}; Array Initializer www.java9s.com
Two dimensional Array • int[][] marks = new int[2][3]; marks[0][0] = 30; marks[0][1]=31; marks[0][2]=32; marks[1][0]=40; marks[1][1]=41; marks[1][2]=42; 1 0 1 2 1 2 0 0 31 40 41 42 32 30 www.java9s.com
Two dimension- non uniform • int[][] marks = new int[2]; • marks[0] = new int[3]; • marks[1] = new int[4]; 1 0 1 2 0 0 1 2 3 www.java9s.com
Multidimensional Arrays • int[][][] marks = new int[2][3][2]; 1 0 1 2 1 2 0 0 0 1 0 1 0 1 0 1 0 1 0 1