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 1. Write a method to search for and remove an element from a linked list,...

    Java 1. Write a method to search for and remove an element from a linked list, thereby returning the data portion of the node. 2. Making use of both Stacks and Queues, write a function that will determine whether or not a String is a palindrome

  • 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...

  • Data Structures and Algorithms (Java Programming) Write a method to search for and remove an element...

    Data Structures and Algorithms (Java Programming) Write a method to search for and remove an element from a linked list, thereby returning the data portion of the node.

  • 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...

  • Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of...

    Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...

  • Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all...

    Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all the tests in the tester file passes. IntegerLinkedList.java implements IntegerNode.java Your task is to implement the interface described in the file IntegerList.java using a doubly linked list in the file IntegerLinkedList.java. Your implementation must be a doubly-linked list that maintains both a head and a tail reference. You've also been provided with a node class (IntegerNode.java) that includes a prev and next references. //IntegerList.java...

  • 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...

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