logo

Relleno de matrices Java

Mediante el uso el Arrays.fill() método, podemos llenar una matriz completa o podemos llenar una parte de ella. matrices.rellenar() El método también es capaz de llenar matrices 2D y 3D.

El método arrays fill() tiene la siguiente sintaxis:

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

Aquí,

  1. El llegar El parámetro define una matriz que se va a llenar.
  2. El índice de formulario El parámetro define el índice del primer elemento que se completará con el valor dado.
  3. El al Indice El parámetro define el índice del último índice que se completará con el valor dado.
  4. El vale El parámetro define el valor que se almacenará en todos los elementos de la matriz dada.

Tomemos algunos ejemplos para comprender cómo podemos usar el método Arrays.fill() para llenar una matriz.

Llenar toda la matriz 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;>