If we have to swap two elements in java then we can swap them using a temporary variable.
int temp=a;
a=b;
b=temp;
Also, we can do it by using different ways also but is there any predefined method like C++ that have std::swap()
?
In the collection, we have swap()
but it only works for the list
.