logo

Java Arrays Fill

Používaním the Arrays.fill() metódou, môžeme vyplniť buď celé pole, alebo môžeme vyplniť jeho časť. Arrays.fill() metóda je tiež schopná vyplniť 2D aj 3D polia.

Metóda Arrays fill() má nasledujúcu syntax:

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

Tu,

  1. The arr parameter definuje pole, ktoré sa má vyplniť.
  2. The formIndex parameter definuje index prvého prvku, ktorý má byť naplnený danou hodnotou.
  3. The toIndex parameter definuje index posledného indexu, ktorý sa má vyplniť danou hodnotou.
  4. The val parameter definuje hodnotu, ktorá sa má uložiť do všetkých prvkov daného poľa.

Uveďme si niekoľko príkladov, aby sme pochopili, ako môžeme použiť metódu Arrays.fill() na vyplnenie poľa.

Vyplňte celé 1-D pole

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;>