Arrays fill with multidimensional array in Java

0 votes

Without using a loop, how can I fill a multidimensional array in Java? I tried:

double[][] arr = new double[20][4];
Arrays.fill(arr, 0);

The outcome is java.lang. ArrayStoreException: Double in Java

Nov 16, 2022 in Java by Ashwini
• 5,430 points
702 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

This is so that you don't accidentally assign 0.0 to a double[][] array of double[]s (which would be equivalent to double[] vector = 0.0). Java doesn't actually have any proper multidimensional arrays.

In Java, 0.0 is the default value for doubles, so when you retrieve the matrix from new, it will actually already be zeroed out. However, you could do the following if you wanted to fill it with, let's say, 1.0:

I don't think the API offers a way to deal with this issue without using a loop. However, using a for-each loop to complete the task is straightforward.

double[][] matrix = new double[20][4];

// Fill each row with 1.0
for (double[] row: matrix)
    Arrays.fill(row, 1.0);
answered Nov 16, 2022 by Tejashwini
• 3,820 points

edited Mar 5

Related Questions In Java

0 votes
1 answer

Join array elements with a separator in Java

Using Java 8, you can easily perform ...READ MORE

answered Aug 16, 2018 in Java by geek.erkami
• 2,680 points
3,236 views
0 votes
0 answers

How do I fill arrays in Java?

Is there a proper way to fill ...READ MORE

Aug 12, 2022 in Java by krishna
• 2,820 points
756 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 82,211 views
+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,220 points
3,785 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,140 points
1,859 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
941 views
0 votes
0 answers

Matrix multiplication using arrays

Utilizing multidimensional arrays, I'm attempting to create ...READ MORE

Aug 5, 2022 in Java by krishna
• 2,820 points
715 views
0 votes
0 answers

Multidimensional Arrays lengths in Java

How can I determine the lengths of ...READ MORE

Aug 8, 2022 in Java by krishna
• 2,820 points
630 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
6,099 views
0 votes
2 answers

How does random shuffling of an array

Here is a simple way using an ArrayList: List<Integer> ...READ MORE

answered Nov 2, 2018 in Java by Sushmita
• 6,920 points
1,013 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP