logo

Java masīvu aizpildīšana

Izmantojot Arrays.fill() metodi, mēs varam aizpildīt visu masīvu vai arī kādu tā daļu. Arrays.fill() metode spēj arī aizpildīt gan 2D, gan 3D masīvus.

Arrays fill() metodei ir šāda sintakse:

 Java.util.Arrays.fill(boolean[] arr, int fromIndex, int toIndex, boolean val ) 

Šeit,

  1. The arr parametrs nosaka aizpildāmo masīvu.
  2. The formIndex parametrs nosaka pirmā elementa indeksu, kas jāaizpilda ar doto vērtību.
  3. The indeksēt parametrs definē pēdējā indeksa indeksu, kas jāaizpilda ar doto vērtību.
  4. The val parametrs nosaka vērtību, kas jāsaglabā visos dotā masīva elementos.

Apskatīsim dažus piemērus, lai saprastu, kā masīva aizpildīšanai var izmantot metodi Arrays.fill().

Aizpildiet visu 1-D masīvu

ArraysFillExample1.java

 // import required classes and packages import java.util.Arrays; import java.util.Scanner; // create ArraysFillExample1 to fill sub-array to given array public class ArraysFillExample1 { //main() method start public static void main(String[] args) { int size, ele; Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the size of the array: &apos;); size = sc.nextInt(); //creates an array of size 100 int[] array = new int[100]; System.out.println(&apos;Enter the elements in the array: &apos;); for(int i = 0; i <size; i++) { array[i]="sc.nextInt();" } system.out.print('enter the element which you want to fill in array: '); ele="sc.nextInt();" complete array with arrays.fill(array, ele); system.out.println('after filling' +ele+ ' array:
' + arrays.tostring(array)); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/03/java-arrays-fill.webp" alt="Java Arrays Fill"> <h3>Fill some part of 1-D Array</h3> <p> <strong>ArraysFillExample2.java</strong> </p> <pre> // import required classes and packages import java.util.Arrays; import java.util.Scanner; // create ArraysFillExample2 to fill some part of the array with the given value public class ArraysFillExample2 { //main() method start public static void main(String[] args) { int size, ele, index1, index2; Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the size of the array: &apos;); size = sc.nextInt(); //creates an array of size 100 int[] array = new int[100]; System.out.println(&apos;Enter the elements in the array: &apos;); for(int i = 0; i <size; i++) { array[i]="sc.nextInt();" } system.out.print('enter the element which you want to fill in array: '); ele="sc.nextInt();" system.out.print('from index '+ele+' index1="sc.nextInt();" system.out.print('to index2="sc.nextInt();" complete array with arrays.fill(array, index1, index2, ele); system.out.println('after filling' +ele+ ' array:
' + arrays.tostring(array)); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/03/java-arrays-fill-2.webp" alt="Java Arrays Fill"> <h3>Fill multidimensional array (2D Array)</h3> <p>Just like a single-dimensional array, we can also fill the multidimensional array by using the Arrays.fill() method. In order to fill a multidimensional array, we use the for loop to fill each row of the multidimensional array.</p> <p>Let&apos;s take an example to understand how we can fill a multidimensional array using the Arrays.fill() method.</p> <p> <strong>ArraysFillExample3.java</strong> </p> <pre> // import required classes and packages package javaTpoint.MicrosoftJava; import java.util.Arrays; import java.util.Scanner; // create ArraysFillExample3 to fill the multidimensional array with the given value public class ArraysFillExample3 { //main() method start public static void main(String[] args) { int ele; // create scanner class object Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the size of the array: 
&apos;); System.out.print(&apos;Enter size of row : 
&apos;); int rows = sc.nextInt(); System.out.print(&apos;Enter size of column : 
&apos;); int columns = sc.nextInt(); // create an array of size row*column int twoDArray[][] = new int[rows][columns]; System.out.println(&apos;Enter the elements in the array: 
&apos;); for(int i = 0; i <rows; i++) { for(int j="0;" < columns; j++) system.out.println('enter element ['+i+']['+j+']'); twodarray[i][j]="sc.nextInt();" } system.out.print('enter the which you want to fill in array: 

'); ele="sc.nextInt();" system.out.println('multi-dimensional array after inserting elements: system.out.println(arrays.deeptostring(twodarray)); complete with for (int[] row : twodarray) arrays.fill(row, ele); system.out.println('after filling' +ele+ ' array:

' + arrays.deeptostring(twodarray)); sc.close(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/03/java-arrays-fill-3.webp" alt="Java Arrays Fill"> <p> <strong>ArraysFillExample4.java</strong> </p> <pre> // import required classes and packages package javaTpoint.MicrosoftJava; import java.util.Arrays; import java.util.Scanner; // create ArraysFillExample4 to fill 3D array with the given value public class ArraysFillExample4 { //main() method start public static void main(String[] args) { int ele; // create scanner class object Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the size of the array: 
&apos;); System.out.print(&apos;x : &apos;); int x = sc.nextInt(); System.out.print(&apos;y : &apos;); int y = sc.nextInt(); System.out.print(&apos;z : &apos;); int z = sc.nextInt(); // create an array of size row*column int threeDArray[][][] = new int[x][y][z]; System.out.println(&apos;Enter the elements in the array: &apos;); for(int i=0; i <threedarray.length; i++){ for(int j="0;" < threedarray[i].length; j++){ k="0;" threedarray[i][j].length; k++){ system.out.println('enter element ['+i+']['+j+']['+k+']'); threedarray[i][j][k]="sc.nextInt();" } system.out.print('enter the which you want to fill in array: 

'); ele="sc.nextInt();" system.out.println('3d array after inserting elements: system.out.println(arrays.deeptostring(threedarray)); complete with for (int[][] newrow : threedarray) { (int[] newrowcolumn newrow) arrays.fill(newrowcolumn, ele); system.out.println('after filling' +ele+ ' array:

' + arrays.deeptostring(threedarray)); sc.close(); pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/03/java-arrays-fill-4.webp" alt="Java Arrays Fill"> <hr></threedarray.length;></pre></rows;></pre></size;></pre></size;>