Java Array Assignment: Two Methods Explained

There are two ways of assigning values to arrays in Java.

  1. Direct assignment: It is possible to directly assign a value to each element of the array. For example:
int[] nums = new int[3];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
  1. Utilize looping for assigning values: Loop structures can be used to assign values to each element by iterating through an array. For example:
int[] nums = new int[3];
for (int i = 0; i < nums.length; i++) {
    nums[i] = i + 1;
}

The operation of assigning values to an array can be achieved using any method.

bannerAds