logo

Java-arrays vullen

Door het gebruiken van de Arrays.fill() methode kunnen we een volledige array vullen of een deel ervan. Arrays.fill() methode is ook in staat om zowel de 2D- als de 3D-arrays te vullen.

De methode Arrays fill() heeft de volgende syntaxis:

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

Hier,

  1. De arr parameter definieert een array die moet worden gevuld.
  2. De formulierIndex parameter definieert de index van het eerste element dat moet worden gevuld met de opgegeven waarde.
  3. De indexeren parameter definieert de index van de laatste index die moet worden gevuld met de opgegeven waarde.
  4. De val parameter definieert de waarde die moet worden opgeslagen in alle elementen van de gegeven array.

Laten we enkele voorbeelden nemen om te begrijpen hoe we de methode Arrays.fill() kunnen gebruiken voor het vullen van arrays.

Vul de volledige 1D-array

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