• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

difference between set and add method of list interface

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a doubt regarding List interface in collection. There are two methods provided in the List interface.

E set(int index, E element);
void add(int index, E element);

The difference which i can point out is set returns the element while add do not.

Can anyone help me out to differentiate among these two and when to use which one.

regards,
Ajay Mittal
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
set replaces the element at the given index. add inserts the element at the given index and moves all elements ahead of it one position.
 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:set replaces the element at the given index. add inserts the element at the given index and moves all elements ahead of it one position.



Thanks dear

Ajay
 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:set replaces the element at the given index. add inserts the element at the given index and moves all elements ahead of it one position.



Hi Joanne,
SO we must prefer set instead of add in some situations..?
 
Marshal
Posts: 25832
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saral Saxena wrote:

Joanne Neal wrote:set replaces the element at the given index. add inserts the element at the given index and moves all elements ahead of it one position.



SO we must prefer set instead of add in some situations..?



Well, yes. Perhaps it isn't immediately obvious... but if you want to replace a particular element, then you should prefer the "set" method. Whereas if you want to insert an element, then you should prefer the "add" method.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saral Saxena wrote:SO we must prefer set instead of add in some situations..?


That totally depends on what your program needs to do. Ofcourse in some situations you would need to use set, and in other situations you would need to use add. Without knowing what your program is supposed to do, it is impossible to know which of those is appropriate to use.
 
Marshal
Posts: 70327
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saral Saxena wrote: . . . SO we must prefer set instead of add in some situations..?

Yes, just as we prefer a car to a boat in some situations and a boat to a car in other situations. You use a car or a boat if you are going to different places. You use set and add because you are doing different things.
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic