Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to initialize a 2d array in Java?

int[][] a = {
      {1, 2, 3}, 
      {4, 5, 6, 9}, 
      {7}, 
};
Comment

2d array java

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

2d array java

 int[][] arr = new int[10][20]; 
        arr[0][0] = 1; 
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 :: default constructor java 
Java :: java max array 
Java :: testng with cucumber 
Java :: executors java 
Java :: how to compare two characters in java 
Java :: Caused by: android.view.InflateException: Binary XML file line 
Java :: varargs java 
Java :: method in java 
Java :: get last day of month java 
Java :: java pair class 
Java :: call activity method from adapter 
Java :: find number of weeks between two dates in java 
Java :: how to set default color in android studio 
Java :: Search a 2D Matrix II 
Java :: how to import borderlayout 
Java :: maven set repository location command line 
Java :: input 3 int 1 line in java 
Java :: java gerüst 
Java :: bukkit e.getCurrentItem() bytes? 
Sql :: mysql set safe mode off 
Sql :: postgres active connections 
Sql :: oracle find all tables with column name 
Sql :: mysql where column not integer 
Sql :: could not find driver (SQL: PRAGMA foreign_keys = ON;) 
Sql :: how to edit table name in mysql 
Sql :: SQLSTATE[HY000] [1049] Unknown database 
Sql :: installer postgresql sur ubuntu 
Sql :: sql dateadd hours 
Sql :: how to set an already made tables auto increment in mysql 
Sql :: primary key reset in SQL database 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =