Question

What is wrong with the following code? How would you fix it? (Assume all the appropriate...

What is wrong with the following code? How would you fix it? (Assume all the appropriate import statements)

/* print every other element in a linkedlist */

LinkedList<String> exampleLL = new LinkedList<String>();
exampleLL.add("Today");
exampleLL.add("is");
exampleLL.add("a");
exampleLL.add("sunny");
exampleLL.add("day");
exampleLL.add("outside");
​​​​​​​exampleLL.add("I");
​​​​​​​exampleLL.add("Like");
​​​​​​​exampleLL.add("summer");
​​​​​​​exampleLL.add("and");
​​​​​​​exampleLL.add("the");
​​​​​​​exampleLL.add("sunshine");
​​​​​​​exampleLL.add("it");
​​​​​​​exampleLL.add("brings");
​​​​​​​exampleLL.add("too;");
/* etc etc etc */

ListIterator iter = exampleLL.ListIterator();

System.out.println("Every other word of the speach:")

while (iter.hasNext()) {
System.out.println(iter.next().next());
}

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

import java.util.Iterator;
import java.util.LinkedList;

public class Itr {
   public static void main(String[] args) {
       /* print every other element in a linkedlist */

       LinkedList<String> exampleLL = new LinkedList<String>();
       exampleLL.add("Today");
       exampleLL.add("is");
       exampleLL.add("a");
       exampleLL.add("sunny");
       exampleLL.add("day");
       exampleLL.add("outside");

       /* etc etc etc */
       // listIterator is not available for LinkedList
       //only iterator is available
       Iterator iter = exampleLL.iterator();

       System.out.println("Every other word of the speach:");
       // we need to call only next on this
       while (iter.hasNext()) {
       System.out.println(iter.next());
       }


   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
What is wrong with the following code? How would you fix it? (Assume all the appropriate...
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
  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • Describe what is wrong with the syntax of each the following blocks of code, and show...

    Describe what is wrong with the syntax of each the following blocks of code, and show how to fix it. while answer != 'q': answer = input('Word? ') print(len(answer))

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • What is wrong with the following code? This piece of code incorrectly attempts to remove all...

    What is wrong with the following code? This piece of code incorrectly attempts to remove all even values from a stack integers. How would you fix it? You will need to write a class with the main(String[] args) {….} method to test the following piece of code, find the problem, and fix it.                while (!s1.isEmpty()){                               int n = s1.pop();                               if (n % 2 != 0) {                                 s1.push(n); // if odd put it back.                               }                }

  • What is wrong with the following program? Explain how you will fix it in the code....

    What is wrong with the following program? Explain how you will fix it in the code. #include int main() { int i; int *ptr = &i; scanf("%d", &ptr); printf("The value of i is: %d\n", *ptr); return 0; }

  • what is wrong with my code. what should I do with this kind of error 61...

    what is wrong with my code. what should I do with this kind of error 61 QueueDemo java - Files - RaProjects/one- A Queue Demoava Queueinterface Java inport java.util.LinkedList; import java.util.Queue public class QueueDemo public static void main(String[] args) { 10 11 12 13 Queue String myQueue new LinkedlistString();//Create a reference to a queue Interface myqueue.add("A");//Call the enqueue method on myQueue passing the string value of "A" myQueue.add("B");//call the enqueue method on nyQueue passing the String value of "3" myQueue.add("C");//Call...

  • Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly...

    Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding...

    Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...

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