java - difference between add and set in arraylist -


this question has answer here:

i need please tell me difference between add() , set() in arraylist. wrote program uising set() , add() try find out try fing find out differences search on net not find suitable answer

public class arraylistdemo  {      public static void main(string[] args) throws exception {          arraylist al = new arraylist();         al.add(10);         al.add("a");         al.add("b");         al.add(null);           al.set(0, 11);         system.out.println("after add "+""+al);          al.add(1, "aa");         system.out.println("using add method"+ " " +al);          al.set(1, "aa");         system.out.println("using set method"+ " " +al);     }  } 

o/p- using add method [11, aa, b, null] using set method [11, ac, b, null]

the add() method adds value end of list. set() used replace existing value in specific index in list.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -