Sunteți pe pagina 1din 2

Computer Programming 2

JAVA Arrays
Introduction to Arrays
Suppose we have here three variables of type int with different identifiers for each variable.
int number1;
int number2;
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 obect, we need to use a constructor for this.
. A constructor is a method that is called to create a certain obect.
%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 obect
a$es 3 new int'144(;
or, can also be written as,
22declare and instantiate obect
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 2 0 2 C A Computer Programming 2
: e r c i v a l A . ) e r n a n d e !
number1 3 1;
number2 3 2;
number3 3 3;
Sample 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 Element
%o access an array element, or a part of the array, you use a number called an index or a subscript
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.
public class #avaApplication1 6
public static void main0Strin$'( ar$s1 throws "?@*ception 6
int'( a$e 3 new int'2(;
AufferedBeader r 3 new AufferedBeader0new "nputStreamBeader0System.in11;
int *,a$e1;
for 0*34;*C31;*DD1
6 System.out.print0E@nter A$e, E1;
a$e'*( 3 "nte$er.parse"nt0r.readFine011; 9
int y;
for 0y34;yC31;yDD1
6 System.out.print0EA$eEDa$e'y(DEGnE 1; 9
9
9
Coding !uidelines
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(H %his will result in an array.inde*.out.of.bounds e*ception.
3. Bemember, 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%amelengt#
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; iCa$es.len$th; iDD 16
8 System.out.print0 a$es'i( 1;
I 9 9 9
2 | C S 2 0 2 C A Computer Programming 2
: e r c i v a l A . ) e r n a n d e !

S-ar putea să vă placă și