Question

method in java

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

import java.util.*;

public class Main

{

LinkedList<String> linkedlist = new LinkedList<String>();

public static void main(String[] args) {

int choice;

int position;

Scanner scan = new Scanner(System.in);

Main object = new Main();

String inputElement = "";

while(true){

System.out.println("\nEnter your choice ");

System.out.println(" 1. To add element to list ");

System.out.println(" 2. Remove an element from list ");

System.out.println(" 3. Exit ");

choice = scan.nextInt();

switch(choice){

case 1:

System.out.println("\nEnter the input element ");

inputElement = scan.next();

object.addElements(inputElement);

break;

case 2:

System.out.println("\nEnter the position of element to remove");

position = scan.nextInt();

object.removeAt(position);

break;

default :

System.out.println("Exiting");

System.exit(0);

}

}

}

public void addElements(String value){

// Add elements to LinkedList

linkedlist.add(value);

  

// list before delete at position

System.out.println("LinkedList Elements : ");

for(String str: linkedlist){

System.out.print(str);

System.out.print("->");

}System.out.println();

}

public void removeAt(int position){

if(position<0){

System.out.println("IllegalArgumentException");

}else if(position>linkedlist.size()){

System.out.println("ArrayIndexOutOfBoundsException");

}else {

// Removing the element in the given postion

Object e1 = linkedlist.remove(position);

System.out.println("\nElement "+ e1+ " removed from the list\n");

  

// LinkedList elements after remove

System.out.println("After removal:");

for(String str2: linkedlist){

System.out.print(str2);

System.out.print("->");

}System.out.println();

}

}

}

Enter your choice 1. To add element to list 2. Remove an element from list 3. Exit 2 Enter the position of element to remove

Add a comment
Know the answer?
Add Answer to:
method in java 2. removeAt(pos): remove element at position pos. If position exceeds the length of...
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
  • Problem 1 Enhance the LinkedList class by adding following methods to do some more operations of...

    Problem 1 Enhance the LinkedList class by adding following methods to do some more operations of the linked list: 1. insertAt(pos, data): inserts the element data at position pos of the list. If position exceeds the length of the list throw an exception. Example: List: 4 672 insertAt(2, 8) List: 4 6872 insertAt(0, 9) List: 946872 insertAt(-1,10) IllegalArgumentException List: 94 6872 insertAt(8, 12) IndexOutOfBoundsException List: 94 6872

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) {...

    C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) { if (data[i].Equals(element)) return i; } return -1; } You must complete the Vector<T> implementation by providing the following functionality: void Insert(int index, T item) Inserts a new element into the data structure at the specified index. This will involve four parts (Note a part may be more than a single line of code): o If Count already equals Capacity (eg the currently allocated space...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Given an integer x which is assumed to be in the list a, write a method...

    Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of x in a. Positions are counted as 0,1,2,.... If x does not appear in the list, you should throw an IllegalArgumentException. static int find(int x, List<Integer> a) Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of u in a....

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

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