linked list - Adding a Point in LinkedList without overwriting another Point - Java -


this question has answer here:

i want insert value (10,10) @ index 1 of linkedlist ar. want value (1,1) move on node make space (10,10). fastest way of doing that? can use different data structure better results?

    linkedlist<point> ar = new linkedlist<point>();     ar.add(new point(0,0));     ar.add(new point(1,1));     ar.add(new point(2,2));     ar.set(1,new point(10,10));     (int i=0;i<3; i++){     system.out.println(ar.get(i));     } 

the set(...) method replaces items. should instead use add(int index, object o) overload method insert item @ specific index , not replace.

the price of using method linkedlist depends on how far need iterate target destination. arraylist price potentially creating bigger backing array , shifting elements in indices after target. (always check corresponding list implementation.)


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -