Generics All of the classes in the Java Colections Framework (JCF) use generics.
ID: 3919265 • Letter: G
Question
Generics All of the classes in the Java Colections Framework (JCF) use generics. As we saw w ith the cosparable interface, one purpose of this is that it lats us specify w hich type of objects we are deaing w ith (we can think of the class as having a parameter that spec?ies a type inside it). Note that w hen using generics we can only use reference data types. Java does not alow us to use primitive data types w ith generics (and this is one very good reason we have the primitive w rapper classes). We w ill cover generics in more detail in class later ArrayList The arraylist is a commorly used class in the the JCF It implements the list abstract data type (ADT) https:docs.oracle.comjavase/8idocs/apijavali ArrayList.hm Open up a web brow ser and see what methods the ArrayList class provides. In particular, look at add(E eadint index, E e) get(int index), renove(int index), set(int index, E e?, and size(). Note, E is the type you specify when creating the ArrayList (as follow s) isport java.uts.Arraylist eapty string 1ist ( is String here) ArrayListeString slist new ArraylisteStrin0 eapty integer list (E is Integer here)* ArrayListeinteger ilist new ArrayListeinteger> Modiy your TesELL program so that for each linked list in the program you have a mirror list that is an ArrayList. Use the same operations (w hen applcable) to create the ArrayLists. 2. Overload the sase method to take an LList and an ArraylisteString> that checks if tw o lists are the same.Explanation / Answer
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
public class TestLL {
public static void main(String[] args) {
// create an empty linked list
LinkedList<String> list = new LinkedList<String>();
System.out.print("empty list : ");
System.out.println(list);
// create a list with one element
// list = [cat]
list.add("cat");
System.out.print("singleton : ");
System.out.println(list);
// add some elements to the back and front
list.addFirst("dog");
list.addFirst("owl");
list.addLast("bat");
System.out.print("some adds : ");
System.out.println(list);
// abritrary adds
try {
list.add(1, "crow");
} catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
try {
list.add(1, "goat");
} catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
try {
list.add(2, "eel");
} catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.print("more adds : ");
System.out.println(list);
// some gets
System.out.println("x0 = " + list.get(0));
System.out.println("x1 = " + list.get(1));
System.out.println("x5 = " + list.get(5));
System.out.println("xn = " + list.get(list.size() - 1));
// removes
list.removeFirst();
list.removeFirst();
System.out.println("removes : " + list);
// removes
list.remove(3);
list.remove(list.size() - 1);
System.out.println("removes : " + list);
// remove front add to back
System.out.println("before : " + list);
System.out.println("move front to back ");
list.addLast(list.removeFirst());
System.out.println("after : " + list);
// Array list with mirror elements as LinkedList list
ArrayList<String> alist = new ArrayList<String>();
for (String s : list) {
alist.add(0, s);
}
System.out.println("Linked List list= " + list);
System.out.println("Mirror of Linked List list =" + alist);
LinkedList<String> l1 = new LinkedList<String>();
l1.add("a");
ArrayList<String> l2 = new ArrayList<String>();
l2.add("a");
try {
l1.addFirst("b");
l1.addLast("c");
l1.add("d");
l1.addFirst("b");
l1.addLast("c");
l1.add("eeee");
} catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
// Array list with mirror elements as LinkedList l1
ArrayList<String> alist1 = new ArrayList<String>();
for (String s : l1) {
alist1.add(0, s);
}
System.out.println("Linked List l1= " + l1);
System.out.println("Mirror of Linked List l1 =" + alist1);
// System.out.println("l1.compareTo(l2) = " + l1.c l1.compareTo(l2));
// uncomment this next line
System.out.println("same(l1,l2) = " + same(l1, l2));
}
public static boolean same(LinkedList<String> ll, ArrayList<String> al) {
int flag = 0;
if (ll.size() != al.size())
return false;
int i = al.size();
Iterator<String> itr = al.iterator();
while (itr.hasNext()) {
String str = itr.next();
if (!str.equals(ll.get(i)))
flag = -1;
}
if (flag == -1)
return false;
else
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.