Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my 2D array: 

int[][] matrix = new int[10000][1000];

The program frequently uses: 

Arrays.fill(int[] a, int fromIndex, int toIndex, int val);

It’s to fill a row and column. When I want to fill a row 200 by 1 from 10 to end, I implement the following command: 

Arrays.fill(matrix[200], 10, 1000, 1);

I want to know how to fill a column without for(). Can anyone tell me if there is an efficient data structure that performs both operations with speed? 

1 Answer

0 votes
by (19.7k points)

Below is my source code for Arrays.fill():

public static void fill(int[] a, int fromIndex, int toIndex, int val) {

    rangeCheck(a.length, fromIndex, toIndex);

    for (int i=fromIndex; i<toIndex; i++)

        a[i] = val;

}

You can see that it’s a for a loop. So for columns too, you’ll implement the same way as Array.fill() code. 

Interested in Java? Check out this Java tutorial by Intellipaat. 

Related questions

0 votes
1 answer
+11 votes
2 answers
asked May 23, 2019 in Java by prachi95 (1.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 4, 2021 in Java by sheela_singh (9.5k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...