Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Ragged/Jagged Arrays

807607Oct 12 2006 — edited Oct 13 2006
hey guys i was wondering if you guys could help me a bit^^.

http://bingweb.binghamton.edu/~cs140/Homework/assignment06.html

this is the assignment that i have to do and i am stuck on #3.

here is my code for ArrayDataVer2


package assig6;

public class ArrayDataVer2
{
private double[][] sourceArrayOfArrays;
private double[][] targetArrayOfArrays;
private double highestLength = 0;
private int j = 0;
private int numRows;
private int i = 0;
private double[][] twoDimArrVar;
// This will show as unused until you write
// the method fillOutTheArrays
private double targetArraysFillValue = 0;

/**
* Make targetArrayOfArrays a rectangular array (i.e. not a
* ragged array), with the same numbers of rows as sourceArrayOfArrays.
* The number of columns of targetArrayOfArrays should be the maximum
* length of the rows of sourceArrayOfArrays.
* Also copy any values in sourceArrayOfArrays to the corresponding positons
* in targetArrayOfArrays. Put targetArraysFillValue in all the other
* positions of targetArrayOfArrays.
* For an example, see the graphic in the assignment.
*/
public void fillOutTheArrays()
{
//i is the array number
//j is the size of the array number

// double[][] twoDimArrVar; //MAKING 2 ARRAYS INSIDE AN ARRAY
//numRows = 4 + (int)Math.round((6*Math.random())); //DOING RANDOM = 4-10 ARRAYS
//twoDimArrVar = new double[numRows][]; //MAKING AN ARRAY WITH numRows ELEMENTS

//for (int i = 0; i < numRows; i++)
// {
// int length = (int)Math.round((Math.random()*12)); //MAKE ARRAY LENGTHS FROM 0-12
// twoDimArrVar[i] = new double[length]; //MAKING THE ARRAY WITH AND INDEX OF SIZE LENGTH
//
// for (int j = 0; j < length; j++)
/// {
////// double d = 100*(Math.random() - 0.5); //CREATING A RANDOM NUMBER
// now round it to 3 places of decimals
/// int k = (int)Math.round(d*1000); //ROUND IT TO 3RD DECIMAL PLACE
/// twoDimArrVar[i][j] = k/1000.0; //ADD
// }
//}

double arrayTemp[];
int x = 0;
int arrayTempLength = 0;

for (int i = 0; i < twoDimArrVar.length; i++)
{
arrayTemp = new double[twoDimArrVar[i].length];
arrayTemp = twoDimArrVar[i];
twoDimArrVar[i] = new double[(int)highestLength];
arrayTempLength = arrayTemp.length;

while (arrayTempLength <= (x + 1))
{
twoDimArrVar[i][x] = x;
x++;
}
while (arrayTempLength >= x)
{
twoDimArrVar[i][x] = (int)targetArraysFillValue;
//twoDimArrVar[i][x] = twoDimArrVar[i][targetArraysFillValue];
x++;
}
}
}
/**
* Find the maximum length of any "row" of the array of arrays
* @param array a ragged two dimensional array
* @return the maximum value of array[i].length
* over all rows i, where 0 <= i <= array.length
*/
public int maxArrayLength(double[][] array)
{
int max = 0;
// ...
return max;
}
/**
* Simple constructor uses a method to create a random
* array of arrays
*
*/
public ArrayDataVer2()
{
newArrays();
}
/**
* Call for the creation of the source array by using
* <code>resetSizeAndContent</code> and
* copy the source array to a new target array.
*/
public void newArrays()
{
sourceArrayOfArrays = resetSizeAndContent();
int numRows = sourceArrayOfArrays.length;
targetArrayOfArrays = new double[numRows][];
for(int i = 0; i < sourceArrayOfArrays.length; i++)
{
targetArrayOfArrays[i] = sourceArrayOfArrays[i].clone();
}
}
/**
* Create a two-dimensional array, i.e. an array of arrays
* of random size containing random numbers at each position.
* The array created is ragged
* (every row can have a different length)
*
* @return a random ragged 2-dimensional array
*/


public double[][] resetSizeAndContent()
{
//MAKING 2 ARRAYS INSIDE AN ARRAY
numRows = 4 + (int)Math.round((6*Math.random())); //DOING RANDOM = 4-10 ARRAYS
twoDimArrVar = new double[numRows][]; //MAKING AN ARRAY WITH numRows ELEMENTS

for (i = 0; i < numRows; i++)
{

int length = (int)Math.round((Math.random()*12)); //MAKE ARRAY LENGTHS FROM 0-12
twoDimArrVar[i] = new double[length]; //MAKING THE ARRAY WITH AND INDEX OF SIZE LENGTH
if (length > highestLength) highestLength = length;
for (j = 0; j < length; j++)
{
double d = 100*(Math.random() - 0.5); //CREATING A RANDOM NUMBER
// now round it to 3 places of decimals
int k = (int)Math.round(d*1000); //ROUND IT TO 3RD DECIMAL PLACE
twoDimArrVar[i][j] = k/1000.0; //ADD
}
}
return twoDimArrVar;
}
// GETTER METHODS USED BY ArrayViewVer2
public int getNumSourceRows()
{
return sourceArrayOfArrays.length;
}
public int getNumSourceColumns(int row)
{
return sourceArrayOfArrays[row].length;
}
public int getNumTargetRows()
{
return targetArrayOfArrays.length;
}
public int getNumTargetColumns(int row)
{
return targetArrayOfArrays[row].length;
}
public double getSourceArrayValue(int row, int column)
{
return sourceArrayOfArrays[row][column];
}
public double getTargetArrayValue(int row, int column)
{
return targetArrayOfArrays[row][column];
}
public void setFillValue(double d)
{
targetArraysFillValue = d;
}
}




when i try to run this, it comes up with multiple errors. does anyone know how to correct this?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 10 2006
Added on Oct 12 2006
62 comments
1,359 views