0

How can I insert?

ort java.util.LinkedList; public class MyClass { public static void main(String[ ] args) { LinkedList<String> c = new LinkedList<String>(); c.add("Red"); c.add("Blue"); c.add("Green"); c.add("Orange"); c.remove("Green"); // can I insert for example yellow after blue in that moment? } }

17th Sep 2016, 5:11 PM
Александр Евдокимов
Александр Евдокимов - avatar
3 ответов
+ 5
LinkedList.add(int location,E element); e.g. c.add(2,"yellow");
17th Sep 2016, 5:19 PM
Ahmad Samy
Ahmad Samy - avatar
+ 1
you can find the index of "blue" like this: int getIndex(String element,List list){ for(int i=0;i<list.size();i++){ if(element.equals(list.get(i))return i; } } or just : list.indexOf("blue");
17th Sep 2016, 5:25 PM
Ahmad Samy
Ahmad Samy - avatar
0
c.add(2,"ellow");
17th Sep 2016, 7:31 PM
Ankit Panwar
Ankit Panwar - avatar