java - Remove the object LinkedList -



java - Remove the object LinkedList -

i have created programme regarding linkedlist , here 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 = new scanner(system.in); system.out.println("size: "); s = a.nextint(); system.out.println("element: "); e = a.nextint(); // process (n = 0; n < s; n++) { k.add(n); } // output system.out.println("index of " + e + k); } }

here output:

size: 12 element: 12 index of 12[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

the programme running. want remove 1 element. example, want remove 0 or number in index of 12. how can that?

you should utilize next methods

k.remove(); k.remove(index); k.removefirst(); k.removelast();

here short illustration 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 , removes head (first element) of list. system.out.println("list after k.remove(): " + k); k.remove(2); //removes element @ specified position in list. system.out.println("list after k.remove(2): " + k); k.removefirst();//removes , returns first element list. system.out.println("list after k.removefirst():" + k); k.removelast();//removes , returns lastly element 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]

java linked-list

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -