Sei sulla pagina 1di 2

C S 3 0 2 E M Object Oriented Programming

JAVA Arrays
Introduction to Arrays Suppose we have here three variables of type int with different identifiers for each variable. number1 3 1; int number1; number2 3 2; int number2; number3 3 3; int number3; As you can see, it seems like a tedious task in order to ust initiali!e and use the variables especially if they are used for the same purpose. "n #ava and other pro$rammin$ lan$ua$es, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. %his type of variable is called an array. An array stores multiple data items of the same data type, in a conti$uous block of memory, divided into a number of slots

%o declare an array, write the data type, followed by a set of s&uare brackets'(, followed by the identifier name. )or e*ample, int '(a$es; or int a$es'(; Array Instantiation After declarin$, we must create the array and specify its len$th with a constructor statement. +efinitions, - "nstantiation . "n #ava, this means creation - /onstructor . "n order to instantiate an ob ect, we need to use a constructor for this. . A constructor is a method that is called to create a certain ob ect. %o instantiate 0or create1 an array, write the new keyword, followed by the s&uare brackets containin$ the number of elements you want the array to have. )or e*ample, 22declaration int a$es'(; 22instantiate ob ect a$es 3 new int'144(; or, can also be written as, 22declare and instantiate ob ect int a$es'( 3 new int'144(; 5ou can also instantiate an array by directly initiali!in$ it with data. )or e*ample, int arr'( 3 61, 2, 3, 7, 89; %his statement declares and instantiates an array of inte$ers with five elements 0initiali!ed to the values 1, 2, 3, 7, and 81. 1 C S 3 0 2 E M Object Oriented Programming :ercival A. )ernande!

Sam!"e Program 22creates an array of boolean variables with identifier esults. %his array contains 7 elements that are initiali!ed to values 226true, false, true, false9 boolean results[] = { true, false, true, false }; 22creates an array of 7 double variables initiali!ed to the values 6144, ;4, <4, =89; double []grades = {100, 90, 80, 75}; 22creates an array of Strin$s with identifier days and initiali!ed. %his array contains = elements String days[] = { on!, "ue!, #ed!, "$u!, %ri!, Sat!, Sun!}; Accessing an Array E"ement %o access an array element, or a part of the array, you use a number called an inde# or a subscri!t$ inde* number or subscript - assi$ned to each member of the array, to allow the pro$ram to access an individual member of the array. - be$ins with !ero and pro$ress se&uentially by whole numbers to the end of the array. - >?%@, @lements inside your array are from 4 to 0si!e?fArray.11. %he followin$ is a sample code on how to print all the elements in the array. %his uses a for loop, so our code is shorter. 1 public class ArraySample6 2 public static void main0 Strin$'( ar$s 16 3 int'( a$es 3 new int'144(; 7 for0 int i34; iA144; iBB 16 8 System.out.print0 a$es'i( 1; C 9 = 9 <9 Coding %uide"ines 1. "t is usually better to initiali!e or instantiate the array ri$ht away after you declare it. )or e*ample, the declaration, int '(arr 3 new int'144(; is preferred over, int '(arr; arr 3 new int'144(; 2. %he elements of an n.element array have inde*es from 4 to n.1. >ote that there is no array element arr'n(D %his will result in an array.inde*.out.of.bounds e*ception. 3. Eemember, 5ou cannot resi!e an array. Array &engt' "n order to $et the number of elements in an array, you can use the len$th field of an array. %he len$th field of an array returns the si!e of the array. "t can be used by writin$ ( array)ame$"engt' 1 public class ArraySample 6 2 static void main0 Strin$'( ar$s 16 3 int'( a$es 3 new int'144(; 7 for0 int i34; iAa$es.len$th; iBB 16 8 System.out.print0 a$es'i( 1; C9 =9 9

2 C S 3 0 2 E M Object Oriented Programming :ercival A. )ernande!

Potrebbero piacerti anche