Question
One way to implement the class, ArrayList , is with a static array. Recall the ArrayList implements the List interface and has the following methods:
One way to implement the class, ArrayList
boolean add(int element)
void add(int index, E element)
E get (int index)
int remove( int index)
int indexOf( E element)
int set( int index, E element)
int size( )
String toString()
The E in ArrayList
To implement the ArrayList, well need a static array of integers and an index of where we will store the next element.
When we add an integer without a specified index, it implies that we add it to the end of the current list. Well store the value at next and then increment the next index by 1.
If we add an integer at a specified index (less than next), well first shift all elements from index to next-1 to the right, and then store the element at index. For example, we have a 7 element array with 5 elements:
The command ref.add(2, 8) will first shift all elements from 2 to next-1 to the right, store the 8 into the element at index 2, and then increment the next index.
The shift right will consist of 3 moves, starting at next-1:
After the 3 moves, the 8 is put into the element at index 2 and the next index is incremented:
To remove an element at a specific location, youll need to shift all elements from index+1 to next-1 to the left. Then you will decrement the next index. For example, to remove the element at index 3, we would call: ref.remove(3). Starting with the array above:
Shift the elements starting at index+1 to the left:
Then decrement the next index:
The method size() returns the current number of elements in the array, which is the value of next. In the above case, size() returns 5. We have 5 elements in the array.
When adding an element, it is possible for the static array to fill up. We detect this condition when next is equal to the length of the static array. In this case, create a new array that is twice the length of the static array, copy all elements, and then set the reference of the internal array to the new array. For example, lets say our current static array (ref is arr) is full, so next is equal to arr.length:
Create a new array, nArr, that is twice the length of arr:
Copy all elements from arr to nArr:
Then set the internal reference, arr, to nArr:
well create a new class, ArrayListInteger, which will implement the interface Csc202_List. Csc202_List specifies the following methods:
boolean add(int element)
void add(int index, int element)
int get (int index)
int remove( int index)
int indexOf( int element)
int set( int index, int element)
int size( )
String toString()
Steps:
Create a new Project, ArrayList, in Eclipse.
Create a new Package, staticArray in src (select src, press right mouse button, select new->package).
Copy Csc202_List.java and TestMyArray.java from blackboard into a temporary directory. Select (click on) the package, staticArray, and then import Csc202_List.java. You will need to need to specify the package in the first line of the file:
staticArray;
Select src, and import TestMyArray.java. Make sure you clear out the package field so that it will create a default package. If you forgot how to do this, please ask me or a fellow student. When you finished, your project should look like the following:
Select the staticArray package and create a new ArrayListInteger class. This will implement the interface Csc202_List. When you create the ArrayListInteger class, add the interface in the dialog box and it will stub out the method headers for you.
Your ArrayListIntegers instance variables will include a reference to a static integer array and an integer index, next:
private int [ ] arr;
private int next = 0;
Create a 1 argument constructor, that accepts an integer size, which allows you to specify the initial length of the array, arr.
Create a default constructor, that will create an initial array size of 10 elements. You constructor should call the 1 argument constructor with the following call:
this( 10 );
Implement all the method specified in the Csc202_List interface.
In your methods, if you detect an illegal operation, display (using System.out.println() ) Exception: state reason, ignoring operation. For example, if you tried to add an element at a specific index that is greater than next, the array would not be changed and the following message would be displayed: Exception: invalid index - ignoring add
Use TestMyArray.java to test your implementation.
Create an additional test, which will attempt to add and remove an element at an invalid index. Verify you get a message that indicates the error. In a future lab, your code will generate an Exception.
Create an additional test which will call the ArrayListIntegers toString() method. Use the following method:
public String toString() { String str = ""; for (int k=0; k { str += String.format("%4d,", arr[k]); if ((k+1) %10 == 0) str += String.format(" "); } return str; } |
** As you add code and debug, use
After you have your ArrayListInteger implemented, change the test code so that we use ArrayList
Comment out test 8, because we do not yet handle the Exception. Note, there are 2 places in the code where you need to change:
public static void printArray(String msg, ArrayListInteger a)
And where we initially allocate the ArrayList in main().
Here is code for ArrayListinter.java
import java.util.*;
import staticArray.ArrayListInteger;
public class TestMyArray
{
/**
* @param args
*/
// note, this method tests the ArrayList size() method
public static void printArray(String msg, ArrayListInteger a)
//public static void printArray(String msg, ArrayList
{
if (msg != null)
System.out.println(msg+ ":");
for (int k=0; k { System.out.printf("%4d,", a.get(k)); if ((k+1) %10 == 0) System.out.println(); } System.out.println(); } public static void printTestHeader( int testNum, String msg ) { System.out.println(" ================================================================="); System.out.println("Test: " + testNum + ". " + msg + " "); } public static void printTestTrailer( ) { System.out.println("================================================================= "); } public static void main(String[] args) { int testCount = 1; int element=0; /* * PLEASE READ: uncomment each test as you go to simplify debugging. * Note printArray calls the ArrayList methods: get(index) and size(), so start off implementing * the constructors, size, get, and add. * * After you complete all tests, comment out the line where we create an ArrayListInteger and * uncomment out the line where we create an ArrayList * printArray. Verify you get the same test results. */ printTestHeader(testCount++, "create an ArrayList, add 2 elements, then call size()"); ArrayListInteger myList = new ArrayListInteger(); //ArrayList myList.add(32); myList.add(16); int size = myList.size(); System.out.println("size of array is: " + size); printArray("myList", myList); printTestTrailer(); printTestHeader(testCount++, "test get(index)"); element = myList.get(0); System.out.println("element at 0: " + element); element = myList.get(1); System.out.println("element at 1: " + element); printTestTrailer(); printTestHeader(testCount++, "add several more elements, going past the size of the orignal internal array"); for (int k=0; k<10; k++) myList.add(k + 5); printArray(null, myList); printTestTrailer(); printTestHeader(testCount++, "add elements at specific indices. -11 @ 0, -22 @ 5, and -33 at the end"); myList.add(0, -11); myList.add(5, -22); myList.add(myList.size(), -33); printArray(null, myList); printTestTrailer(); printTestHeader(testCount++, "test set method, set 1st element to 100, middle to 555 and last to 999 "); element = myList.set(0, 100); System.out.println("previous value at 0 was: " + element); int middle = (myList.size()-1)/2; element = myList.set(middle, 555); System.out.println("previous value at " + middle + " was: " + element); element = myList.set(myList.size()-1, 999); System.out.println("previous value at last index was: " + element); printArray(null, myList); printTestTrailer(); printTestHeader(testCount++, "test indexOf method, find specified element at which index "); int index = myList.indexOf(12); System.out.println("found element 12 at: " + index); index = myList.indexOf(100); System.out.println("found element 100 at: " + index); index = myList.indexOf(999); System.out.println("found element 999 at: " + index); index = myList.indexOf(-22); System.out.println("found element -22 at: " + index); index = myList.indexOf(123); System.out.println("found element 123 at: " + index); printTestTrailer(); //add some additional test cases for the exception conditions.... } } heres is the code for CSC202_List.java public interface Csc202_List { void add(int index, int element); boolean add(int element); int get(int index); int remove(int index); int set(int index, int element); int size(); int indexOf(int element); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started