About 153,000 results
Open links in new tab
  1. Different Ways To Declare And Initialize 2-D Array in Java

    Nov 13, 2024 · When you initialize a 2D array, you must always specify the first dimension(no. of rows), but providing the second dimension(no. of columns) may be omitted. Java compiler is smart enough to manipulate the size by checking the number of elements inside the columns.

  2. Initialising a multidimensional array in Java - Stack Overflow

    Jul 1, 2009 · String[][] myStringArray = new String [x][y]; is the correct way to initialise a rectangular multidimensional array. If you want it to be jagged (each sub-array potentially has a different length) then you can use code similar to this answer.

  3. Syntax for creating a two-dimensional array in Java

    You can initialize this array at compile time like below: int[][] multD = {{2, 4, 1}, {6, 8}, {7, 3, 6, 5, 1}}; You can easily iterate all elements in your array:

  4. Java Multi-Dimensional Arrays - GeeksforGeeks

    Apr 23, 2025 · Two-Dimensional Array (2D-Array) Two-dimensional array is the simplest form of a multidimensional array. A 2-D array can be seen as an array storing multiple 1-D array for easier understanding. Syntax (Declare, Initialize and Assigning) // Declaring and Intializing data_type[][] array_name = new data_type[x][y]; // Assigning Value

  5. How to Initialize an Array in Java? - GeeksforGeeks

    Apr 14, 2025 · In this article, we will discuss different ways to declare and initialize an array in Java. Understanding how to declare an array in Java is very important. In Java, an array is declared by specifying its data type, and an identifier, and adding brackets [] …

  6. Java Multi-Dimensional Arrays - W3Schools

    To create a two-dimensional array, add each array within its own set of curly braces: myNumbers is now an array with two arrays as its elements. To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array.

  7. Declare and initialize two-dimensional arrays in Java

    Oct 12, 2023 · To initialize a two-dimensional array of characters, we can use the String.toCharArray() method in Java. This method converts the given string into a sequence of characters and returns a newly created character array.

  8. How to declare and Initialize two dimensional Array in Java

    You can declare 2 dimensional array where each sub array is of different length because its not mandatory to specify length of second dimension while declaring 2D array in Java. This way you can initialize 2D array with different length sub array as shown below : String[][] squares = new String[3][]; squares[0] = new String[10];

  9. java - Initialize 2D array - Stack Overflow

    Dec 12, 2012 · If it is to simply initialize a 2D array to values 0-9, it would be much easier to just define, declare and initialize within the same statement like this: private char[][] table = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

  10. Initialize 2D array in Java - Java2Blog

    Jan 11, 2021 · There are several ways to create and initialize a 2D array in Java. Let’s see some examples. Initialize 2D array Using for loop. This is the simplest approach in which we create an array and initialize every index using for loop. Output: Initialize 2D array using an initializer.

Refresh