ArrayAndArrayList
Work on this exercise by yourself,
but
be quick to ask questions of your instructor, student assistants and classmates as desired.
Goals
The goals of this exercise are to apply and hence reinforce your understanding of:
- array
- Not a class
- Declaring
- Size versus capacity (x.length). Cannot grow.
- Filling, traversing. Extended for loops.
- Arrays class
- ArrayList
- Is a class, generic. Implements Collection.
- Declaring, constructing
- Size (x.size()) versus capacity. Grows automatically.
- Filling, traversing. Extended for loops.
- Collections class
- Using the debugger in Eclipse.
Overview
You will checkout a program that reads integers from standard input
and prints them in sorted order (smallest to largest).
The version you checkout will use arrays.
After examining and running the version that uses arrays,
you will extend the program to do the same thing (read and sort)
but using ArrayList instead of arrays.
Also, you will use the extended for loop where appropriate.
Stage 0 - Checkout the Project
- Open Eclipse.
- Checkout your ArrayAndArrayList project.
Stage 1: Examine and run the version with an array
- Examine the code. Note especially:
- How the program gets inputs.
- How the program terminates input.
- How the array is:
- Declared
- Filled
- Sorted
- Printed
Run the program, providing about 5 inputs.
Put some of the inputs on one line, some on another.
Consider:
If you enter non-numeric input, no error is given;
the program simply treats the non-numeric input as signalling
the end of the input. This is not a great user interface,
but treat it as acceptable.
The program does have an egregious error, however.
What inputs (hint: or number of inputs) can you supply to make that egregious error occur?
Stage 2: Implement a version that uses ArrayList
- Write a static void method called
GetSortAndPrintAnArrayList
that:
- Accomplishes the same thing as GetSortAndPrintAnArray.
- Uses an ArrayList instead of an array.
- Uses an extended for statement
wherever appropriate.
Comment out the call in main to GetSortAndPrintAnArray,
add a call in main to GetSortAndPrintAnArrayList,
and test your work.
Consider:
List the two major advantages of an ArrayList
over an array.
Consider:
Which is generally preferable: using an ArrayList
or using an array?
Stage 3: Experience the debugger in Eclipse
- Experience the debugger in Eclipse by doing all of the following:
- Set breakpoints (before and after the sort)
- Run in debug mode
- Examine the variables at the first breakpoint.
- Continue to the next breakpoint.
- Examine the variables at the second breakpoint. Note yellow-highlighted.
- Disable and (eventually) remove all breakpoints
Stage last: Turn in your work (i.e., commit your project)
- Make sure that your project has correct style.
Make sure that there is appropriate HTML documentation.
- Commit your ArrayAndArrayList project to the repository from which you checked it out.