In this module, we have seen how an ArrayList works by performing operations using an underlying
array. In this assignment, you will modify the implementation of an ArrayList so that it uses Java
Generics and has additional functionality.
In completing this assignment, you will:
• Understand the implementation of an ArrayList in Java
• Gain familiarity with reading and modifying existing code
• Be sure that you can submit code to the course’s autograding platform
Download the MyArrayList.java ﬁle, which contains the unimplemented methods for the code
that you will write in this assignment. Be sure that you are able to compile this code, and review
the implementation before proceeding.
As you work on this assignment, please do not change the signatures of any of the methods (their
parameter lists, names, and return value types) and do not create any additional .java ﬁles for your
solution. Additionally, please be sure that your MyArrayList class is in the default package, i.e.
there is no “package” declaration at the top of the source code.
For this and all other assignments in this course, we recommend that you work on your local com-
puter using a development environment such as Eclipse, IntelliJ, Visual Studio Code, or whichever
IDE you used for any prior Java courses.
Although you will need to upload your solution to Codio for grading (see “How to Submit” below)
and could develop your solution on that platform, we recommend using industry standard tools
such as Eclipse or IntelliJ so that you become more used to them and can take advantage of their
If you have trouble setting up your IDE or cannot get the starter code to compile, please post a
note in the discussion forum and a member of the instruction staﬀ will try to help you get started.
In this assignment, you will modify the MyArrayList class to give it some extra functionality.
Step 1. Use Java Generics
Modify the MyArrayList class so that it uses Java Generics instead of Strings. That is, it should
be possible to create an instance of the class like this:
MyArrayList<Int e ge r> l i s t = new MyArrayList <>();
You will need to make changes throughout the class deﬁnition. Make sure that all methods accept
or return objects of the parameterized type, and not just Strings.
This step is a bit tricky, so Oracle’s Generics Tutorial is recommended reading. In particular:
• Generic Types
• Generic Methods
• The ﬁne print
We don’t expect you to read every single article in the tutorial, nor to suddenly become an expert
on all of Java’s quirks. You may need some more of these articles in future assignments, but for
now you just need to know what a generic type is, how it’s used in terms of concrete syntax, and
the restriction (particular to Java) that the type of arrays is erased at runtime, so you cannot have
arrays of a generic or parameterized type.
Speciﬁcally, note that the underlying array should not be of type E, and you should NOT write:
We know the above line is what was recommended in the textbook and lectures, but this is a
mistake, even if it seems to work in this case. In real code, trying to cast Object to E leads
to ClassCastExceptions under many circumstances and is considered bad practice.
Instead, declare your underlying array (data) to be of type Object. There are some implications
• When you have a method that takes an element of variable type E and modiﬁes your underlying
array, you can safely store it in the array since Object is the parent class of all other classes.
• When you have a method that returns an element of type E out of the underlying array, you
will be typecasting the element from Object to E, e.g. return (E) obj;.
• Your IDE may warn you about unsafe casts, and it will likely oﬀer to insert an annotation
(@SuppressWarnings(“unchecked”)) to ignore unsafe casts. You need to manually check that
all of your casting is safe before inserting this annotation. Such annotations must be used very
carefully and wisely as they can destroy the type safety of your program if used incorrectly!
Note however that this is actually one of the good uses for SuppressWarnings.
Step 2. Implement remove method
Implement the remove method so that it takes an object of the parameterized type (from Step 1)
as its parameter, and removes the ﬁrst instance of an equivalent element in the underlying array,
i.e. for which the .equals method returns true.
Once the object has been removed, the elements following it should be shifted over to ﬁll the empty
space, and the method should return true; if the object does not exist in the underlying array, the
method should simply return false.
For instance, assume the underlying array contained these values in this order:
“Cat” “Dog” “Banana” “Aardvark” null null
“Cat” “Dog” “Aardvark” null null null
If the method remove(“Banana”) were called, then the array should look like this:
And the method should return true.
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: email@example.com 微信:itcsdx