You are on page 1of 14
UNITS |63 Data stares Suggested Answers to Self-tests @ Feedback Self-test 5.1 Java provides ew constructs, an array and a vector, which appear to be similar, In fact, the array and vector are altogether different. Ie is important to understand the differences between the two, in order to write efficient code. Arrays have fixed size — after creating an array, you cannot add more elements than its maximum size. IF you do, you will get an ArrayTndexOutofBoundsexception. In Java, arrays are objects, so any methods contained in java.Lang. object can be invoked on them. In order to find the length of the array, use the public variable Length. Ane(] da = new ine (0); system.out.println(*ia length is “+ ia.length); ‘Arrays can hold both primitive types and object references. Default values are used for each entry, based on its type. [A vector grows its size dynamically when more elements are added than its current size can accommodate. As elements get deleted, cach clement with an index geeater than the index being removed is shifted downward, Unlike arrays, you call a method on a vector tw determine its size. vector v = new Vector); System.out.printin(*y length is * + v.sizeQ)s ‘The vector’s size measures the number of elements it holds. Thus, may vary, unlike the array’s size which is Fixed. A vector is implemented in terms of an array of java.lang.Object. “That is, when ic grows, or shrinks, the entire array must be reallocared and copied. This may cause performance problems if vectors are not used properly. the vector's 64] WAWASAN OPEN UNIVERSITY 1CC102 Computing it Self-test 5.2 ‘The following table summarises the advantages and disadvantages of an array object and a List object. ‘Aspect Alpe object [an aray object Element ype | Asacoliect Lonobject, | The type of aray element the storage of a ise | can be primitive or non- object can maintain | primitive, and an array relerencesof Object ype | 0b12ct can therefore and cantherefore maintain | Ratlvely maintain 2 ary objects in the Jove | collection of primitive programming language. | Y2** However itcannot natively imaintainprimitivevaluesf | 't 15 possible to restrict necessary tneedswrapper | the type of elements to objects for maintaining | be msintained by an array rrative valves object. For example, an array object with element Asa nist objectcanstore | PE of class Dace can ay object reference tes | only store the reference ntpossibieto restrict the | referring to Dace object objects to be added to a List objec. ficiency | The time to access a | All elements maintained particular maintained | by an array object can be object depends on the | accessed in any order by implementationcsssbeing | using the array subscriot used, may take longerto | oF index, and the time to access particuarelement | access any element i the maintained by a Lie | some, object. Extensibility | The capacity of a Lise | The size ofan aray object object can extend | isdeterminedat thetimeit automatically is created, There is no way to resize an array object and the workaround Is to create another array object with a larger size and copy the contents from the original array object to the naw one, Selftest 5.3 TestArrayList java JJ Resolve classes in java.util package import java.util; HJ Definition of clase TestarrayList public clase TestArraytist | (1 Main executive method UNITS Datasets public static void main(steiagi) arse) { J/ check program parameter Af (args.lenath <1) ( 11 show usage system. out printin(*Usage: "); ) else ( 11 prepare a List object (a ArrayList object) List objtist = new Arraybist() // Add element to the List object objList-add(0, “Hello “); objtist-add(1, args (01) objtist-add(2, ™. Tt is now“); objList.add(3, new Date()) objList-add (a, *.")5 17 Show the objects maintained by the List one 11 by one Iterator iterator = objList-iterator(); while (iterator-hasNext()) { syetem.out.print (Aterator-next()) y system.out.printin(); Selftest 5.4 ‘The expression for obraining the sum is: Antobj1.intValue() + intobj2.intvalue() To obtain the seeing object wich contents "12", either one of the following expressions can be used: AntOnjl.tostring(} + intob}2-testring() AntObjl.tostring(} + intob32 Antobjl + intobj2.tostring() wv 4 intob}2 + intOdj2

You might also like