I know java is pass by value But here How the array[i] is changing the value of arr[i].The below program is arranging zeros and ones in order
Is there any special case for Array-Pass by value
class GFG {
// function to segregate 0s and 1s
static void segregate0and1(int arry[], int n)
{
int count = 0; // counts the no of zeros in arr
for (int i = 0; i < n; i++) {
if (array[i] == 0)
count++;
}
// loop fills the arr with 0 until count
for (int i = 0; i < count; i++)
array[i] = 0;
// loop fills remaining arr space with 1
for (int i = count; i < n; i++)
array[i] = 1;
}
// function to print segregated array
static void print(int arr[], int n)
{
System.out.print("Array after segregation is ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// driver function
public static void main(String[] args)
{
int arr[] = new int[]{ 0, 1, 0, 1, 1, 1 };
int n = arr.length;
segregate0and1(array, n);
print(array, n);
}
}