0% found this document useful (0 votes)
7 views2 pages

Jagged Array Methods in Java

This document discusses useful methods for working with arrays in Java. It provides an example method to calculate the sum of a column in a jagged array. It also demonstrates how to use the Arrays.fill method to initialize all elements of a 2D array to a specific value when constructing a Matrix object.

Uploaded by

pline13579
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Jagged Array Methods in Java

This document discusses useful methods for working with arrays in Java. It provides an example method to calculate the sum of a column in a jagged array. It also demonstrates how to use the Arrays.fill method to initialize all elements of a 2D array to a specific value when constructing a Matrix object.

Uploaded by

pline13579
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays useful methods:

Sum of Columns in a jagged array


public static int addColumn(int[][] arr, int x){
int columnSum = 0;
int i = 0;
int j = 0;
int numRows = [Link];
//add column values to find sum

while( i < numRows ){
if( x < arr[i].length ){
columnSum += arr[i][x];
}
++i;
}
return columnSum;

Fill a 2D array with a certain value
fill(double[] a, double val)
Assigns the specified double value to each element of the specified array of doubles.

//other variances of fill method to fill, int, etc and even objects/obj references/ fill in only for a
certain range etc.


public Matrix(int r, int c, double fillIn){
element=new double [r][r];
for (double[] arr: element){
[Link](arr, fillIn);
}
}

You might also like