Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * interface for a list * * ordered collection with duplicates allowed */ public interface List { void add(T item); boolean remove (T item);

image text in transcribed

/* * interface for a list * * ordered collection with duplicates allowed */ public interface List { void add(T item); boolean remove (T item); boolean contains(T item); int size(); String toString(); // list-specific methods void add (int index, T item); T get (int index); T remove (int index); T set(int index, T item); int indexOf(T item); } 

/* * ArrayList * * array-based implementation of a list * * ordered collection with duplicates allowed */ public class ArrayList implements List { public static final int DEFAULT_CAPACITY = 10; private T [] collection; private int size; /* * no-argument constructor * * size set to default value */ public ArrayList() { this(DEFAULT_CAPACITY); } /* * argument constructor * * size specified by user */ @SuppressWarnings("unchecked") public ArrayList(int capacity) { collection = (T[]) new Object[capacity]; size = 0; } /* * adds item to list */ public void add (T item) { checkForNull(item); ensureSpace(); collection[size] = item; size++; } /* * removes item from list * * returns true if item found in list */ public boolean remove (T item) { // to be implemented return false; } /* * returns true if item is in list */ public boolean contains (T item) { for (int i = 0; i = size) { throw new IndexOutOfBoundsException("index outside of list!"); } } /* * shifts items one to right starting at specified index */ private void shiftRight (int index) { for (int i = size; i > index; i--) { collection[i] = collection[i-1]; } } /* * shifts items one to left starting at specified index */ private void shiftLeft (int index) { for (int i = index; i  

/* * LinkedList * * linked implementation of a list * * ordered collection with duplicates allowed */ public class LinkedList implements List { private class Node { private T data; private Node next; public Node(T item) { data = item; next = null; } } private Node head; private Node tail; private int size; public LinkedList() { head = null; tail = null; size = 0; } /* * adds item to list */ public void add (T item) { checkForNull(item); Node newest = new Node (item); if (size == 0) { head = newest; } else { tail.next = newest; } tail = newest; size++; } /* * removes item from list * * returns true if item found in list */ public boolean remove (T item) { // to be implemented return false; } /* * returns true if item is in list */ public boolean contains (T item) { Node current = head; for (int i = 0; i = size) { throw new IndexOutOfBoundsException("index out of range!"); } } /* * returns pointer to node at specified index */ private Node getNode (int index) { Node current = head; for (int i = 0; i  

/* * test program for lists * * can be run with ArrayList or LinkedList */ public class Lists { public static void main (String [] args) { try { List words = new ArrayList(); //List words = new LinkedList(); // adding items System.out.println(" adding 'hello' to the list"); words.add("hello"); System.out.println(words); System.out.println(" adding 'world' to the list"); words.add("world"); System.out.println(words); // adding at a specific position System.out.println(" adding 'there' at position 1"); words.add(1, "there"); System.out.println(words); System.out.println(" adding 'well' at position 0"); words.add(0, "well"); System.out.println(words); // replacing System.out.println(" replacing item at position 1 with 'hi'"); System.out.println("replaced: " + words.set(1, "hi")); System.out.println(words); // retrieving System.out.println(" getting item at position 3"); System.out.println(words.get(3)); // contains and indexOf System.out.println(" does the list contain 'hello'?"); System.out.println(words.contains("hello") ? "yes" : "no"); System.out.println("what is its index?"); System.out.println(words.indexOf("hello")); System.out.println(" does the list contain 'hi'?"); System.out.println(words.contains("hi") ? "yes" : "no"); System.out.println("what is its index?"); System.out.println(words.indexOf("hi")); // removing item System.out.println(" --> testing remove methods"); System.out.println(words); System.out.println("removing 'hi'"); words.remove("hi"); System.out.println(words); // removing at specified index System.out.println(" removing item at index 1"); System.out.println("removed: " + words.remove(1)); System.out.println(words); System.out.println(" removing item at index 0"); System.out.println("removed: " + words.remove(0)); System.out.println(words); // item not in list System.out.println(" trying to remove 'hello'"); System.out.println("was it removed? " + ((words.remove("hello"))? "yes" : "no")); System.out.println(words); System.out.println(" trying to remove 'world'"); System.out.println("was it removed? " + ((words.remove("world"))? "yes" : "no")); System.out.println(words); // index out of range System.out.println(" trying to remove item at index 0"); words.remove(0); } catch (IndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } } 

A list is an ordered collection, with duplicates allowed. In class we implemented methods for adding, replacing, and retrieving an item from a given position, as well as for getting the index of an item. Add methods to remove a specific item and to remove an item at a given index to both ArrayList and LinkedList boolean remove (T item) T remove (int index); Then compile and run Lists.java. The output that tests the two remove methods should look like this ->testing remove methods [wel, hi, there, world] removing hi' [well, there, world] removing item at index 1 removed: there well, world removing item at index 0 removed: well [world) trying to remove 'hello' was it removed? no [world] trying to remove 'world was it removed? yes [l trying to remove item at index 0 index outside of list

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

Explain the factors influencing wage and salary administration.

Answered: 1 week ago

Question

Examine various types of executive compensation plans.

Answered: 1 week ago

Question

1. What is the meaning and definition of banks ?

Answered: 1 week ago

Question

2. What is the meaning and definition of Banking?

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago