Get Instant Help From 5000+ Experts For
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing:Proofread your work by experts and improve grade at Lowest cost

And Improve Your Grades
myassignmenthelp.com
loader
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Guaranteed Higher Grade!
Free Quote
wave
Collections in Java: ArrayList, HashMap, and Arrays

Declaring and Creating Collection Variables

Collections: ArrayList, HashMap,  arrays

1.Declare and create variables capable of storing collections of values or collections of references to objects. These variables may be objects of a library class or arrays. Remember: with an array you must include the type of value/object you intend to store (the compiler will not allow values/objects of different types to be added to the array)

2.Add/Get items from your collection.  Some library classes have methods to do this, but these methods may require additional information e.g. the index of the object/value giving its position in the collection, or a reference to the object. 

Arrays do not have any built in methods - you have to write all of it yourself.

In many cases, to process an item in a collection, you will need to find it first using "linear search". It is worth making this into a local method

Remember : all collections should be "typed" to specify what class of objects they store e.g. private ArrayList<Course> allCourses

3.Remember that HashMap is a map, were each item in the map consists of  a pair: a key and a value. To add to a HashMap, you need to give two items (the method is called "put", rather than the "add" of ArrayList). 

The two items may be: 

two objects from classes which you wish to link e.g. a Student and a Tutor (you need to decide which is going to be the key and which the value) 

two objects from the same class but representing different information e.g. a String which represents a name and a String which represents a tel.no. 

One object (as the value) and one of its one object fields as the key e.g. a course code(a String) as the key and the Course itself as the value (course code being a field of Course)You can retrieve an item from the HashMap by specifying its key.

Iterate through a collection to process all the items in the collection in some way, or process selected items in some way. This always requires you to use loops (while.. or for…). Control of the loop can be done by using the index (if the collection is indexed e.g. ArrayList, arrays) ,  - while or for, or the special loop for collections for (ElementType holder: CollectionName)

Selective processing always requires you to have an if.. inside the loop.

Describe the main features of each type of collection: 

ArrayList -  indexed, ordered collection of objects. Essentially, a list where items are usually added at the end. Items may be accessed using the index which gives their position in the list. Inserting an item in the middle of the list automatically "moves down" the items which follow. Removing an item "closes the gap". You can list items in order.

HashMap - an unordered collection of  (key, value) pairs. Each key is unique and is associated with only one value. (adding an item with the same key overwrites the old one). You cannot list items in a predictable order.

arrays - indexed structures which can be used to store basic values (e.g. int, double) or object references. Must be declared with a fixed size which cannot be changed during a program run (library class collections can grow/shrink as required). The only built in feature is a field called length which returns the declared size of array. Data can be written/ accessed directly using an indexed position. Data can be listed in order of the index (but there may be problems with null references)

5. Identify the similarities and differences between different types of the collections

6. Select collections according to the requirements of the problem.

a list items "in order" - ArrayList or array

b. pairs of items - HashMap

c. frequent add/remove items (flexible length) - one of the library classes

d. frequent access to items requiring "finding" the item  - HashMap using the key

e. store values of a basic type - array 

f.    fixed size collection - array

support
close