logo

Umplerea matricelor Java

Prin utilizarea the Arrays.fill() metoda, putem fie să umplem o matrice completă, fie să umplem o parte din ea. Arrays.fill() metoda este, de asemenea, capabilă să umple atât matricele 2D, cât și 3D.

Metoda Arrays fill() are următoarea sintaxă:

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

Aici,

  1. The arr parametrul definește o matrice care trebuie completată.
  2. The formIndex parametrul definește indexul primului element care trebuie completat cu valoarea dată.
  3. The laIndex parametrul definește indexul ultimului index care trebuie completat cu valoarea dată.
  4. The val parametrul definește valoarea care trebuie stocată în toate elementele matricei date.

Să luăm câteva exemple pentru a înțelege cum putem folosi metoda Arrays.fill() pentru umplerea matricei.

Umpleți întreaga matrice 1-D

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