Question

The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...

The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program.

import java.util.ArrayList;
import java.util.Iterator;

public class GroceryList {
static public void main(String[] args){
ArrayList<String> groceryList = new ArrayList<String>();
Iterator<String> iter;
  
groceryList.add("apples");
groceryList.add("bread");
groceryList.add("juice");
groceryList.add("carrots");
groceryList.add("ice cream");
  
System.out.println("Number of items on grocery list:" + groceryList.size());
System.out.println("Items are: ");
iter = groceryList.listIterator();
while(iter.hasNext()) {
String nextItem = iter.next();
System.out.println(groceryList.indexOf(nextItem)+ " " + nextItem);
}// end while
  
}//end main
} //end GroceryList
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Hello! here are two programs one is for ArrayList and the other is LinkedList. I used in ArrayList program list methods to maintain a grocery list and method of linked list used in another program. Comments are given in each program. thanks.

import java.util.ArrayList;
import java.util.Iterator;

public class GroceryList //import packages
{
static public void main(String[] args) //main function
{
   int size; //int variable to store size
   String item; //string type varisble to store an item
ArrayList<String> groceryList = new ArrayList<String>(); //create list
Iterator<String> iter;
boolean ans=groceryList.isEmpty(); //boolean type variable to check list
  
if(ans==true) //checking variable
System.out.println("List is Empty");
else
   System.out.println("List is not Empty");

//add items to list

       groceryList.add("apples");
       groceryList.add("bread");
       groceryList.add("juice");
       groceryList.add("carrots");
       groceryList.add("ice cream");
       System.out.println(" ");
          

//printing items of a list

           System.out.println("Number of items on grocery list:" + groceryList.size());
           System.out.println("Items are: ");
           iter = groceryList.listIterator();
           while(iter.hasNext()) {
           String nextItem = iter.next();
           System.out.println(groceryList.indexOf(nextItem)+ " " + nextItem);
           }// end while
//again checking list is empty or not
           System.out.println(" ");
           ans=groceryList.isEmpty();
       if(ans==true) //checking variable
           System.out.println("List is Empty");
       else
           System.out.println("List is not Empty");
       System.out.println(" ");
          
           groceryList.remove("juice"); //remove item "juice"
           System.out.println("Removed Juice Item from list");
           System.out.println(" ");
//after removing display list
           System.out.println("After remove item Modified grocery list:" + groceryList.size());
           iter = groceryList.listIterator();
           while(iter.hasNext()) {
           String nextItem = iter.next();
           System.out.println(groceryList.indexOf(nextItem)+ " " + nextItem);
           }// end while
//size method used to get size of list
           size=groceryList.size(); //size method
           System.out.println("Size of List: "+size);
           System.out.println(" ");
          
           item=groceryList.get(2); //get method use to get item from a list
           System.out.println("we got item of index two: "+item);
          
      
}//end main
} //end GroceryList

import java.util.*;
public class Test //class test
{
public static void main(String args[]) //main function
{
// Creating object of class linked list
LinkedList<String> item = new LinkedList<String>();
  
// Adding items to the linked list
item.add("Apples");
item.add("Bread");
item.addLast("Chocolate");
item.addFirst("Biscuit");
item.add(2, "Juice");
item.add("Shake");
item.add("ButterMilk");
System.out.println("Linked list : " + item);
  
// Removing items from the linked list
item.remove("Bread");
item.remove(3);
item.removeFirst();
item.removeLast();
System.out.println("Linked list after deletion: " + item);
  
// Finding items in the linked list
boolean status = item.contains("Shake");
  
if(status)
System.out.println("List contains the item 'Shake' ");
else
System.out.println("List doesn't contain the item 'Shake'");
  
// Number of items in the linked list (size)
int size = item.size();
System.out.println("Size of linked list = " + size);
  
// Get and set items from linked list
String element = item.get(2);
System.out.println("Item returned by get() : " + element);
item.set(2, "ProteinShake");
System.out.println("Linked list after change : " + item);
}
}

Add a comment
Know the answer?
Add Answer to:
The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>>...

    //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) { // Problem 1 method        // empty linked list, with key=k         if (!containsKey(k)) {               put(k, new LinkedList<V>());         }         // adding v to the linked list associated with key k         get(k).add(v);    }    public V removeFirst(K k)...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • CHALLENGE ACTIVITY 7.16.1: Enter the output of the ArrayList ADT functions. Jump to level 1 Type...

    CHALLENGE ACTIVITY 7.16.1: Enter the output of the ArrayList ADT functions. Jump to level 1 Type the program's output import java.util.ArrayList; import java.util.Scanner; public class IntegerManager ( public static void printSize(ArrayList<Integer> numsList) System.out.println(numsList.size() + "items"); public static void main(String[] args) Scanner scnr = new Scanner(System.in); int currval; ArrayList<Integer> intList = new ArrayList<Integer>(); Input 123-1 printSize (intList); Output currval = scnr.nextInt(); while (currval >= 0) { intList.add(currval); currval = scnr.nextInt(); printSize (intList); intList.clear(); printSize (intList); 1 2 Check Next

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {   ...

    /** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList {    /**    * @param args    */    public static void main(String[] args) {        List groceryList = new ArrayList<>();        groceryList.add("rice");        groceryList.add("chicken");        groceryList.add("cumin");        groceryList.add("tomato");        groceryList.add("cilantro");        groceryList.add("lime juice");        groceryList.add("peppers");               groceryList.remove("cilantro");               for (int i = 0; i            if (groceryList.get(i).equals("peppers")) {...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT