+ 3

ArrayList Implementation - Java

Hey, I tried implementing the Java ArrayList / C++ Vector (Works basically the same). I finished writing the code but i have a little problem. please run the code below and try and help me figure the solution out. code: https://code.sololearn.com/cJeMg1c9NfHl

16th Sep 2021, 1:21 PM
Yahel
Yahel - avatar
12 Respostas
+ 2
The unsafe cast warning occurs because you convert an Object array to T[] You can get rid of the warning by annotating your class, or all the methods that do this unsafe cast, like so: @SuppressWarnings("unchecked") Read this to learn more, about possible alternatives... https://www.baeldung.com/java-generic-array
16th Sep 2021, 5:02 PM
Tibor Santa
Tibor Santa - avatar
+ 2
First mistake is how you use the generic type parameters. You already declare T on the class level (correctly) : public class ArrayList <T> Then your expend, add and remove methods do not need <T> again, this confuses the compiler because now you seem to have 2 different T's. Just this is fine: public void remove(T element)
16th Sep 2021, 3:30 PM
Tibor Santa
Tibor Santa - avatar
+ 1
write back <T> in class declaration
16th Sep 2021, 3:59 PM
zemiak
+ 1
I fixed couple of things and the code works, but I get a Note after the code runs about unchecked and unsafe operations. How can I fix that? Why do I get that?
16th Sep 2021, 4:12 PM
Yahel
Yahel - avatar
+ 1
zemiak, that's how an ArrayList works though... isn't it? (There is also an option to reserve places from the get go, when creating the instance...)
16th Sep 2021, 7:07 PM
Yahel
Yahel - avatar
+ 1
no, in java standard API starts at 0 by first element grows to 10 or initial capacity then grows *1.5
16th Sep 2021, 7:21 PM
zemiak
+ 1
zemiak, Ok, good to know... is it the same with C++ vectors? I fixed my code, you are free to try it :)
16th Sep 2021, 7:23 PM
Yahel
Yahel - avatar
+ 1
zemiak, fixed again :)
16th Sep 2021, 9:06 PM
Yahel
Yahel - avatar
0
Tibor Santa, still doing problems... now it says that it doesn't recognize T... I updated my code, you can run the code again..
16th Sep 2021, 3:38 PM
Yahel
Yahel - avatar
0
expand array by one place is expensive
16th Sep 2021, 6:39 PM
zemiak
0
edit: I checked my affirmation and corrected my last post
16th Sep 2021, 8:58 PM
zemiak
0
ArrayList is a class of Java Collection framework. It uses a dynamic array for storing the objects. It is much similar to Array, but there is no size limit in it. We can add or remove the elements whenever we want. We can store the duplicate element using the ArrayList; It manages the order of insertion internally. The ArrayList class is much more flexible than the traditional Array. It implements the List interface to use all the methods of List Interface. It takes place in java.util package. The ArrayList class inherits the AbstractList class and implements the List Interface. The elements of it can be randomly accessed. It can not be used for primitive types such as int, char, etc.; for these data types, we need a wrapper class. The difference between Array and ArrayList is that Arraylist provides a dynamic array that can be expanded when needed. In Array, we have to specify the size of the Array during initialization, but it is not necessary for ArrayList. By default, it takes its size to 10.
17th Sep 2021, 4:25 AM
Arun Jamson
Arun Jamson - avatar