How to initialize multi-dimensional array in java?

0 votes
I am new to java language. I don't know how initialize multi-dimeansional array. Please help?
Jun 6, 2018 in Java by Akrati
• 3,150 points
73 views

2 answers to this question.

0 votes

You can declare multi dimensional arrays like :

// 4 x 5 String arrays, all Strings are null
// [0] -> [null,null,null,null,null]
// [1] -> [null,null,null,null,null]
// [2] -> [null,null,null,null,null]
// [3] -> [null,null,null,null,null]

String[][] sa1 = new String[4][5];
for(int i = 0; i < sa1.length; i++) {           // sa1.length == 4
    for (int j = 0; j < sa1[i].length; j++) {     //sa1[i].length == 5
        sa1[i][j] = "new String value";
    }
}


// 5 x 0  All String arrays are null
// [null]
// [null]
// [null]
// [null]
// [null]
String[][] sa2 = new String[5][];
for(int i = 0; i < sa2.length; i++) {
    String[] anon = new String[ /* your number here */];
    // or String[] anon = new String[]{"I'm", "a", "new", "array"};
    sa2[i] = anon;
}

// [0] -> ["I'm","in","the", "0th", "array"]
// [1] -> ["I'm", "in", "another"]
String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};
Custom dissertation writing service is the better service that provides detailed and effective information related to educational basis. 
answered Jun 11, 2018 by rebeccablinova
• 140 points
0 votes

a multidimensional array can define an array of an array, the data that store in multidimensional is in tabular form like in a row form, the syntax of initialise the multidimensional array is :

data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];

where:

  • data_type: Type of data to be stored in the array. For example int, char, etc.
  • dimension: The dimension of the array created.
    For example: 1D, 2D, etc.
  • array_name: Name of the array like cheapest essays
  • size1, size2, …, sizeN: Sizes of the dimensions respectively.
answered Mar 18 by monica jesvina

Related Questions In Java

+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
322 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,050 points
42 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
72 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by parth295
• 4,620 points
62 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,880 points
114 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
45 views
0 votes
2 answers

How a static map can be initialized?

public class Test { ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,880 points
70 views
0 votes
1 answer

How to initialize multi-dimensional array in java?

A multidimensional array in Java is really an array ...READ MORE

answered Jun 6, 2018 in Java by sophia
• 1,400 points
33 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,123 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,978 views