Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

2d array java

char[][] array = new int[rows][columns];
Comment

2d array java

 int[][] arr = new int[10][20]; 
        arr[0][0] = 1; 
Comment

2-dimensional Array java

class MultidimensionalArray {
    public static void main(String[] args) {

        // create a 2d array
        int[][] a = {
            {1, 2, 3}, 
            {4, 5, 6, 9}, 
            {7}, 
        };
      
        // calculate the length of each row
        System.out.println("Length of row 1: " + a[0].length);
        System.out.println("Length of row 2: " + a[1].length);
        System.out.println("Length of row 3: " + a[2].length);
    }
}
Comment

define 2d array in java

const testMatrix = [
  [1, 1, 1, 0, 0],
  [1, 1, 1, 0, 1],
  [0, 1, 0, 0, 1],
  [0, 0, 0, 1, 1]
];

const directions = [
  [-1, 0], //up
  [0, 1], //right
  [1, 0], //down
  [0, -1] //left
]

const numberOfIslands = function(matrix) {
  if(matrix.length === 0) return 0;
  let islandCount = 0;

  for(let row = 0; row < matrix.length; row++) {
    for(let col = 0; col < matrix[0].length; col++) {
      if(matrix[row][col] === 1) {
        islandCount++;
        matrix[row][col] = 0;
        const queue = [];
        queue.push([row, col]);

        while(queue.length) {
          const currentPos = queue.shift();
          const currentRow = currentPos[0];
          const currentCol = currentPos[1];

          for(let i = 0; i < directions.length; i++) {
            const currentDir = directions[i];
            const nextRow = currentRow + currentDir[0];
            const nextCol = currentCol + currentDir[1];

            if(nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix[0].length) continue;

            if(matrix[nextRow][nextCol] === 1) {
              queue.push([nextRow, nextCol]);
              matrix[nextRow][nextCol] = 0;
            }
          }
        }
      }
    }
  }

  return islandCount;
}
Comment

PREVIOUS NEXT
Code Example
Java :: palindrome java 
Java :: Java printf() Method using PrintWriter 
Java :: set layout params in dp value 
Java :: press enter in robot java 
Java :: apache csv get headers 
Java :: convert void * to int 
Java :: convert int to byte java 
Java :: java delete object 
Java :: hash table implementation java 
Java :: java random number generator in range 
Java :: H2 enabling remote database creation first 
Java :: Android: remove shadow from bottom navigation 
Java :: how to stop activity from another activity 
Java :: java explicit array declaration 
Java :: java 2 decimals 
Java :: how to convert arraylist to iterable in java 
Java :: Cannot find a class with the main method. 
Java :: enum values to string array 
Java :: count the number of occurrences of a character in a string java 
Java :: selection sort java 
Java :: jpa page sort 
Java :: java hashmap time complexity 
Java :: latest android version 
Java :: java set 
Java :: binary to decimal java 
Java :: how to addin java 
Java :: write content to file java 
Java :: check if duplicate element in java 
Java :: java letter to number 
Java :: build libgdx jar 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =