Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to initialize a 2d array in Java?

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

Java 2d array initialization

class TwoDimensionalArray {

    public static void main(String[] args) {
        String[][] salutation = {
            {"Mr. ", "Mrs. ", "Ms. "},
            {"Kumar"}
        };

        // Mr. Kumar
        System.out.println(salutation[0][0] + salutation[1][0]);

        // Mrs. Kumar
        System.out.println(salutation[0][1] + salutation[1][0]);
    }
}

The output from this program is:

Mr. Kumar
Mrs. Kumar
Comment

how to initialize one dimensional array in java

int[] a; // valid declaration
int b[]; // valid declaration 
int[] c; // valid declaration 
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 :: responseentity spring boot 
Java :: get random number from enum in java 
Java :: add opacity to activity android 
Java :: load file as string java 
Java :: How to perform quick sort, in Java? 
Java :: fileinputstream 
Java :: java secretkey 
Java :: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 
Java :: java eclipse console clear 
Java :: java Write a program to reverse an array or string 
Java :: how to check the end of a string java 
Java :: Duration class java 
Java :: how to find numbers of digits in java 
Java :: java arraylist deepcopy 
Java :: retainall java 
Java :: android application class 
Java :: retrofit android 
Java :: array input java 
Java :: java solid principles 
Java :: icon label java 
Java :: java not equals string 
Java :: how to print byte array in java 
Java :: radio button android:inputType 
Java :: java swing menu click event 
Java :: read timed out android studio 
Java :: set look and feel system default java 
Java :: inheritance in oop 
Java :: restart java windows 
Java :: java toggle button get state 
Java :: how to split string in java android 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =