I\'m Having trouble Creating my ArrayList Class. I cant get it to work or even c
ID: 3769096 • Letter: I
Question
I'm Having trouble Creating my ArrayList Class. I cant get it to work or even compile correctly. I imported all the correct methods but each time I make the neccessary changes it doesnt work.
"Write your own ArrayList class that implements the ListT interface. You will need to write a driver to test your ArrayList. You can test your class by removing or commenting out the import statement of Places.java and running that against your ArrayList. This only tests add, but it may be a good place to start. Driver (client) program should test each function defined in the ListT interface. "
public interface ListT<E> {
/**
* Appends the specified element to the end of this list
* @param data
* @return boolean
*/
boolean add(E data);
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any subsequent
* elements by adding one to their indices.
* @param index - index at which the specified element is to be inserted
* @param data - element to be inserted
* @return boolean
* @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
*/
boolean add(int index, E data);
/**
* Removes all of the elements from this list
*/
void clear();
/**
* Returns true if this list contains the specified element
* @param data
* @return boolean
*/
boolean contains(E data);
/**
* Returns the element at the specified position in this list
* @param index
* @return E
*/
E get(int index) throws Exception;
/**
* Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not
* contain the element
* @param data
* @return int
*/
int indexOf(E data);
/**
* Returns true if this list contains no elements
* @return boolean
*/
boolean isEmpty();
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements by subtracting one from their indices.
* @param index - index of the element to be removed
* @return E - the element that was removed from the list
* IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
*/
E remove(int index);
/**
* Trims the capacity of this ArrayList instance to be the list's current size. An application can use this
* operation to minimize the storage of an ArrayList instance.
*/
void trimToSize();
/**
* Returns the number of elements in this list
* @return int
*/
int size();
}
Explanation / Answer
Could you please check this code ..this is working well
import java.util.*;
public class ArrayListEx {
public static void main(String args[]) {
ArrayList<String> lst = new Arraylst<String>();
lst.add("itm1");
lst.add("itm2");
lst.add("itm3");
lst.add("itm4");
System.out.println("The arraylst contains the following elements: "
+ lst);
int ps= lst.indexOf("itm2");
System.out.println("The index of itm2 is: " + pos);
boolean chk = lst.isEmpty();
System.out.println("chking if the arraylst is empty: " + chk);
int sz = lst.sz();
System.out.println("The sz of the lst is: " + sz);
boolean element = lst.contains("itm5");
System.out
.println("chking if the arraylst contains the object itm5: "
+ element);
String itm = lst.get(0);
System.out.println("The itm is the index 0 is: " + itm);
System.out
.println("Retrieving itms with loop using index and sz lst");
for (int i = 0; i < lst.sz(); i++) {
System.out.println("Index: " + i + " - itm: " + lst.get(i));
}
System.out.println("Retrieving itms using foreach loop");
for (String str : lst) {
System.out.println("itm is: " + str);
}
System.out.println("Retrieving itms using iterator");
for (Iterator<String> it = lst.iterator(); it.hasNext();) {
System.out.println("itm is: " + it.next());
}
lst.set(1, "Newitm");
System.out.println("The arraylst after the replacement is: " + lst);
lst.remove(0);
lst.remove("itm3");
System.out.println("The final contents of the arraylst are: " + lst);
String[] simpleArray = lst.toArray(new String[lst.sz()]);
System.out.println("The array created after the conversion of our arraylst is: "
+ Arrays.toString(simpleArray));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.