Arrays fill 2d array java

Fill a 2D Array in Java

Fill a 2D Array in Java

The 2D array is based on a table structure which means rows and columns, and filling the 2d array cannot be done with simple adding to array operation. This tutorial demonstrates how to fill a 2d array in Java.

Fill a 2D Array in Java

The arrays in Java are zero-based, which means the range of the array is 0 to the array.Length – 1 . To fill the 2D array, we have to start the filling from the index 0 – 0 .

We need to use two-dimensional loops to fill a 2d array. The example below demonstrates how to fill a 2d array in Java.

package delftstack;  import java.util.Scanner; public class Fill_Array   public static void main(String[] args)    System.out.print("Number of rows for 2d array: ");  Scanner input = new Scanner(System.in);  int Row = input.nextInt();   System.out.print("Number of columns for 2d array: ");  int Column = input.nextInt();   //2d array declaration  int[][] Demo_Array = new int[Row][Column];  for (int x = 0; x  Row; x++)   for (int y = 0; y  Column; y++)   System.out.print(String.format("Enter the array member at Demo_Array[%d][%d] : ", x, y));  Demo_Array[x][y] = input.nextInt(); //add the member to specific index  >  >  //Print the 2d Array  for (int x = 0; x  Demo_Array.length; x++)   for (int y = 0; y  Demo_Array[0].length; y++)   System.out.print(Demo_Array[x][y] + "\t");  >  System.out.println();  >  // close the scanner object  input.close();  > > 

The code above will ask to input the number of rows and columns first, and then it asks for the array member at each index.

Number of rows for 2d array: 3 Number of columns for 2d array: 3 Enter the array member at Demo_Array[0][0] : 1 Enter the array member at Demo_Array[0][1] : 2 Enter the array member at Demo_Array[0][2] : 4 Enter the array member at Demo_Array[1][0] : 5 Enter the array member at Demo_Array[1][1] : 6 Enter the array member at Demo_Array[1][2] : 7 Enter the array member at Demo_Array[2][0] : 8 Enter the array member at Demo_Array[2][1] : 9 Enter the array member at Demo_Array[2][2] : 10 1 2 4 5 6 7 8 9 10 

We used a two-dimensional loop to fill the array. It can also be done manually using the Demo_Array[0][0] = number; syntax, but this can be a long way to fill the array.

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

Related Article — Java Array

Источник

Arrays.fill() – Fill Java Arrays

Let us first discuss and demonstrate the fill() method with two arguments.

Arrays.fill(array, value):- It assigns the specified double value to each element of the specified array. Here the given value will be stored in all elements of the given array.

import java.util.Arrays; public class ArrayTest < public static void main(String[] args) < // int array example int arr[] = ; System.out.println("Original = " + Arrays.toString(arr)); Arrays.fill(arr, 99); System.out.println("After filling = " + Arrays.toString(arr)); > >

Original = [10, 20, 30, 40, 50]After filling = [99, 99, 99, 99, 99]

Let us demonstrate some other examples with double, char, and boolean array.

// double array example double doubelArr[] = ; System.out.println("Original = " + Arrays.toString(doubelArr)); Arrays.fill(doubelArr, 18.9); System.out.println("After filling = " + Arrays.toString(doubelArr));

Original = [15.9, 20.5, 30.5, 40.5, 55.9]After filling = [18.9, 18.9, 18.9, 18.9, 18.9]

// char array example char charArr[]=; System.out.println("Original = " + Arrays.toString(charArr)); Arrays.fill(charArr, 'A'); System.out.println("After filling = " + Arrays.toString(charArr));

Original = [K, n, o, w, P, r, o, g, r, a, m]After filling = [A, A, A, A, A, A, A, A, A, A, A]

// boolean array example boolean boolArr[] = ; System.out.println("Original = " + Arrays.toString(boolArr)); Arrays.fill(boolArr, true); System.out.println("After filling = " + Arrays.toString(boolArr));

Original = [true, false, true, false, false]After filling = [true, true, true, true, true]

How Arrays.fill() method is implemented? The Arrays.fill() is using normal for loop to iterate through the array and update the array element by given value. The Arrays.fill(int[] a, int val) is implemented as given below. The remaining fill(array, value) is also implemented in a very similar way.

public static void fill(int[] a, int val)

Arrays.fill(array, fromIndex, toIndex, value)

  • a:- the array to be filled.
  • val:- the value to be stored in all elements of the array.
  • fromIndex:- the index of the first element (inclusive) to be filled with the specified value.
  • toIndex:- the index of the last element (exclusive) to be filled with the specified value.

Let us demonstrate it through some examples.

// int array example int arr1[] = ; // Example1:- filling first 3 elements Arrays.fill(arr1, 0, 3, 99); System.out.println("After filling first 3 elements = " + Arrays.toString(arr1));

After filling first 3 elements = [99, 99, 99, 40, 50]

int arr2[] = ; // Example2:- filling last 3 elements in given array Arrays.fill(arr2, 2, arr2.length, 99); System.out.println("After filling last 3 elements = " + Arrays.toString(arr2));

After filling last 3 elements = [10, 20, 99, 99, 99]

int arr3[] = ; // Example3:- filling all elements in given array Arrays.fill(arr3, 0, arr3.length, 99); System.out.println("After filling all elements = " + Arrays.toString(arr3));

After filling all elements = [99, 99, 99, 99, 99]

  • IllegalArgumentException:- if fromIndex > toIndex
  • ArrayIndexOutOfBoundsException:- if fromIndex < 0 or toIndex > a.length

How Arrays.fill() method with four arguments is implemented? The Arrays.fill(-,-,-,-) is implemented similar to Arrays.fill(-,-) but first it checking the range. If fromIndex, toIndex are valid to perform the operation then only it is going to the next step. It is also using normal for loop to iterate through the array from fromIndex to toIndex and update the array element by given value. The Arrays.fill(int [] a, int val) is implemented as given below. The remaining fill(array, value) is also implemented in a very similar way.

public static void fill(int[] a, int fromIndex, int toIndex, int val) < rangeCheck(a.length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; >static void rangeCheck(int arrayLength, int fromIndex, int toIndex) < if (fromIndex >toIndex) < throw new IllegalArgumentException( "fromIndex(" + fromIndex + ") >toIndex(" + toIndex + ")"); > if (fromIndex < 0) < throw new ArrayIndexOutOfBoundsException(fromIndex); >if (toIndex > arrayLength) < throw new ArrayIndexOutOfBoundsException(toIndex); >>

Filling multidimensional array in Java

The Arrays.fill() method directly can be used only for the single-dimensional array. It doesn’t perform deep operations. To fill multi-dimensional array, we need to take the help of loops and Arrays.fill() method.

Java Program to fill 2D Java array,

import java.util.Arrays; public class ArrayTest < public static void main(String[] args) < // 2D array int matrix[][] = < < 1, 2 >, < 4, 5 >, < 7, 8 >>; // filling all elements with 9 in given 2D array // using for loop for(int i=0; i System.out.println("After filling all elements = " + Arrays.deepToString(matrix)); > >

After filling all elements = [[ 9, 9 ], [ 9, 9 ], [ 9, 9 ]]

// using for each loop for (int[] row : matrix)

Java Program to fill 3D Java array,

// 3D array 2x3x2 int[][][] arr = ,,>,,,>>; // filling all elements with 5 in given 3D array // using for loop for (int i = 0; i < arr.length; i++) < for (int j = 0; j < arr[i].length; j++) < Arrays.fill(arr[i][j], 5); >> System.out.println("After filling all elements = " + Arrays.deepToString(arr));

After filling all elements = [[[ 5, 5 ], [ 5, 5 ], [ 5, 5 ]], [[ 5, 5 ], [ 5, 5 ], [ 5, 5 ]]]

// using for-each loop for (int[][] row : arr) < for (int[] column : row) < Arrays.fill(column, 5); >>

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

Arrays.fill() – Fill Java Arrays

Let us first discuss and demonstrate the fill() method with two arguments.

Arrays.fill(array, value):- It assigns the specified double value to each element of the specified array. Here the given value will be stored in all elements of the given array.

import java.util.Arrays; public class ArrayTest < public static void main(String[] args) < // int array example int arr[] = ; System.out.println("Original = " + Arrays.toString(arr)); Arrays.fill(arr, 99); System.out.println("After filling = " + Arrays.toString(arr)); > >

Original = [10, 20, 30, 40, 50]After filling = [99, 99, 99, 99, 99]

Let us demonstrate some other examples with double, char, and boolean array.

// double array example double doubelArr[] = ; System.out.println("Original = " + Arrays.toString(doubelArr)); Arrays.fill(doubelArr, 18.9); System.out.println("After filling = " + Arrays.toString(doubelArr));

Original = [15.9, 20.5, 30.5, 40.5, 55.9]After filling = [18.9, 18.9, 18.9, 18.9, 18.9]

// char array example char charArr[]=; System.out.println("Original = " + Arrays.toString(charArr)); Arrays.fill(charArr, 'A'); System.out.println("After filling = " + Arrays.toString(charArr));

Original = [K, n, o, w, P, r, o, g, r, a, m]After filling = [A, A, A, A, A, A, A, A, A, A, A]

// boolean array example boolean boolArr[] = ; System.out.println("Original = " + Arrays.toString(boolArr)); Arrays.fill(boolArr, true); System.out.println("After filling = " + Arrays.toString(boolArr));

Original = [true, false, true, false, false]After filling = [true, true, true, true, true]

How Arrays.fill() method is implemented? The Arrays.fill() is using normal for loop to iterate through the array and update the array element by given value. The Arrays.fill(int[] a, int val) is implemented as given below. The remaining fill(array, value) is also implemented in a very similar way.

public static void fill(int[] a, int val)

Arrays.fill(array, fromIndex, toIndex, value)

  • a:- the array to be filled.
  • val:- the value to be stored in all elements of the array.
  • fromIndex:- the index of the first element (inclusive) to be filled with the specified value.
  • toIndex:- the index of the last element (exclusive) to be filled with the specified value.

Let us demonstrate it through some examples.

// int array example int arr1[] = ; // Example1:- filling first 3 elements Arrays.fill(arr1, 0, 3, 99); System.out.println("After filling first 3 elements = " + Arrays.toString(arr1));

After filling first 3 elements = [99, 99, 99, 40, 50]

int arr2[] = ; // Example2:- filling last 3 elements in given array Arrays.fill(arr2, 2, arr2.length, 99); System.out.println("After filling last 3 elements = " + Arrays.toString(arr2));

After filling last 3 elements = [10, 20, 99, 99, 99]

int arr3[] = ; // Example3:- filling all elements in given array Arrays.fill(arr3, 0, arr3.length, 99); System.out.println("After filling all elements = " + Arrays.toString(arr3));

After filling all elements = [99, 99, 99, 99, 99]

  • IllegalArgumentException:- if fromIndex > toIndex
  • ArrayIndexOutOfBoundsException:- if fromIndex < 0 or toIndex > a.length

How Arrays.fill() method with four arguments is implemented? The Arrays.fill(-,-,-,-) is implemented similar to Arrays.fill(-,-) but first it checking the range. If fromIndex, toIndex are valid to perform the operation then only it is going to the next step. It is also using normal for loop to iterate through the array from fromIndex to toIndex and update the array element by given value. The Arrays.fill(int [] a, int val) is implemented as given below. The remaining fill(array, value) is also implemented in a very similar way.

public static void fill(int[] a, int fromIndex, int toIndex, int val) < rangeCheck(a.length, fromIndex, toIndex); for (int i = fromIndex; i < toIndex; i++) a[i] = val; >static void rangeCheck(int arrayLength, int fromIndex, int toIndex) < if (fromIndex >toIndex) < throw new IllegalArgumentException( "fromIndex(" + fromIndex + ") >toIndex(" + toIndex + ")"); > if (fromIndex < 0) < throw new ArrayIndexOutOfBoundsException(fromIndex); >if (toIndex > arrayLength) < throw new ArrayIndexOutOfBoundsException(toIndex); >>

Filling multidimensional array in Java

The Arrays.fill() method directly can be used only for the single-dimensional array. It doesn’t perform deep operations. To fill multi-dimensional array, we need to take the help of loops and Arrays.fill() method.

Java Program to fill 2D Java array,

import java.util.Arrays; public class ArrayTest < public static void main(String[] args) < // 2D array int matrix[][] = < < 1, 2 >, < 4, 5 >, < 7, 8 >>; // filling all elements with 9 in given 2D array // using for loop for(int i=0; i System.out.println("After filling all elements = " + Arrays.deepToString(matrix)); > >

After filling all elements = [[ 9, 9 ], [ 9, 9 ], [ 9, 9 ]]

// using for each loop for (int[] row : matrix)

Java Program to fill 3D Java array,

// 3D array 2x3x2 int[][][] arr = ,,>,,,>>; // filling all elements with 5 in given 3D array // using for loop for (int i = 0; i < arr.length; i++) < for (int j = 0; j < arr[i].length; j++) < Arrays.fill(arr[i][j], 5); >> System.out.println("After filling all elements = " + Arrays.deepToString(arr));

After filling all elements = [[[ 5, 5 ], [ 5, 5 ], [ 5, 5 ]], [[ 5, 5 ], [ 5, 5 ], [ 5, 5 ]]]

// using for-each loop for (int[][] row : arr) < for (int[] column : row) < Arrays.fill(column, 5); >>

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

Читайте также:  Vue js and css
Оцените статью