-2

I have created a program regarding LinkedList and here is the code:

package consoleapplication;

import java.util.Scanner;
import java.util.LinkedList;

public class Index {

static int s, n ,e;

public static void main(String[] args) { 
        // LinkedList
        LinkedList k = new LinkedList();

        // input
        Scanner a = new Scanner(System.in);

        System.out.println("Size: ");
        s = a.nextInt();

        System.out.println("Element: ");
        e = a.nextInt();

        // process
        for (n = 0; n < s; n++) {
            k.add(n);
        }

        // output
        System.out.println("Index of " + e + k);
    }
}

Here is the output:

Size: 12
Element: 12
Index of 12[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

The program is running. But I want to remove one element. For example, I want to remove 0 or any number in the index of 12. How can I do that?

  • 1
  • more specifically, loot at the 3 `remove()` methods – omu_negru Jun 24 '14 at 08:36
  • Please read up on documentation first and mention what you have tried and what didn't work. Make us believe you put some effort into solving the problem on your own. – SirRichie Jun 24 '14 at 08:38
  • Remove method is what you need http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove() – AurA Jun 24 '14 at 08:39
  • possible duplicate of [how to remove a object from linked list in java?](http://stackoverflow.com/questions/10735288/how-to-remove-a-object-from-linked-list-in-java) – AurA Jun 24 '14 at 08:40
  • possible duplicate of [Remove a node in LinkedList in Java given the node reference](http://stackoverflow.com/questions/19920715/remove-a-node-in-linkedlist-in-java-given-the-node-reference) – Alexandru Cimpanu Jun 24 '14 at 08:41

4 Answers4

1
k.remove(indexYouWantToRemove);

For more Information look here: http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#remove%28int%29

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Reporter Jun 24 '14 at 09:06
  • Isn't k.remove(indexYouWantToRemove); the essential part of the answer? – MichaelS Jun 24 '14 at 09:18
  • @reporter the top half looks like an answer to me. Why isn't it? – John Dvorak Jun 24 '14 at 09:24
  • @MichalS The intention of SO is to create an archive of questions and -helpful- answers. Even at Oracle the documentation is effected by changes – Reporter Jun 24 '14 at 09:25
0

Before you implement or ask anything in Java check if it isn't already implemented. These are the methods that could help you:

public boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Alexandru Cimpanu
  • 1,029
  • 2
  • 14
  • 38
0

you should use next methods

    k.remove();
    k.remove(Index);
    k.removeFirst();
    k.removeLast();

Here is a short example about it usage:

import java.util.Arrays;
import java.util.LinkedList;

public class Index {

    public static void main(String[] args) {

        // LinkedList

        LinkedList<Integer> k = new LinkedList<Integer>() {
            {
                addAll(Arrays.asList(new Integer[]{1, 2, 3, 4, 5, 6, 7}));
            }
        };


        System.out.println("Initial List:           " + k);
        k.remove(); //Retrieves and removes the head (first element) of this list.
        System.out.println("List after k.remove():  " + k);
        k.remove(2); //Removes the element at the specified position in this list.
        System.out.println("List after k.remove(2): " + k);
        k.removeFirst();//Removes and returns the first element from this list.
        System.out.println("List after k.removeFirst():" + k);
        k.removeLast();//Removes and returns the last element from this list.
        System.out.println("List after k.removeLast(): " + k);
    }
}

And output:

Initial List:           [1, 2, 3, 4, 5, 6, 7]
List after k.remove():  [2, 3, 4, 5, 6, 7]
List after k.remove(2): [2, 3, 5, 6, 7]
List after k.removeFirst():[3, 5, 6, 7]
List after k.removeLast(): [3, 5, 6]
Volodymyr Dvornyk
  • 1,332
  • 1
  • 12
  • 15
-1

Answer reference http://www.tutorialspoint.com/java/util/linkedlist_remove_object.htm

Description The java.util.LinkedList.remove(Object o) method removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged.

Declaration Following is the declaration for java.util.LinkedList.remove() method

public boolean remove(Object o) Parameters o -- element to be removed from this list, if present

Return Value This method returns true if this list contained the specified element

Exception NA

Example The following example shows the usage of java.util.LinkedList.remove() method.

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {

public static void main(String[] args) {

  // create a LinkedList
  LinkedList list = new LinkedList();

  // add some elements
  list.add("Hello");
  list.add(2);
  list.add("Chocolate");
  list.add("10");

  // print the list
  System.out.println("LinkedList:" + list);

  // remove "10"
  System.out.println("10 is in the list:" + list.remove("10"));

  // print the list
  System.out.println("LinkedList:" + list);

} }

  • This is just a verbatim copy of the page that you linked to. You should summarize the main points of that page instead of just copying the whole thing here. –  Jul 12 '14 at 23:15