Hi - I'm making a program to calculate PI to n number of decimal places, the calculations are all going fine and now I want to combine values from two arrays into one 2D array for quick reference. I'm a-little confused on how to do this. It should go like this:

There will never be more than two columns for each row, but the number of rows can be any number.

xVals is the first array.
quantVals is the second array.
Both are of type double as is the 2D array.

2Darray[0][0] = xVals[0]
2Darray[0][1] = quantVals[0]

Next row, continue as above....

This seems simple enough to me but in actual practice I can't seem to do it. This is what I have so far:

// Number of columns determined by user's choice - the number of rows will always be 
		// 2 for each column because each x value has only one quantity value
		
		int maxCol = 2;
		
		double[][] allVals = new double[length][maxCol];	
		

		for( int i = 0; i < length; i++ )
		{
			for( int j = 0; j < maxCol; j++ )
			{
				allVals[i][j] = xVals[i];
			}
			
		}

I am correct in assuming that the first square brackets when declaring an array determines the number of rows so to speak and the next the number of columns? Therefore if i did this:

int [][] myArray = new int[5][4];

...this would create an array with enough space for 20 values of type integer?

Cheers

Yo - I worked it out - logic & perseverance wins the day :P Didn't position everything properly. I have posted the code and the output:

public static double[][] combineValues( int length, double[] xVals, double[] quantVals )
	{
		// Number of columns determined by user's choice - the number of rows will always be 
		// 2 for each column because each x value has only one quantity value
		
		int maxCol = 2;
		int newLength = length+1;
		int k = 1;
		
		
		double[][] allVals = new double[newLength][maxCol];	
		

		for( int i = 0; i < newLength; i++ )
		{
			for( int j = 0; j < 1; j++ )
			{
				// System.out.println( "x value ---- "+xVals[i] );
				allVals[i][j] = xVals[i];
			}
			// System.out.println( "quant value ---- "+quantVals[i] );
			allVals[i][k] = quantVals[i];
		}
		
		print2D( allVals );
		return allVals;
	}

Ouput
Row 0 Column 0 ---- 0.0
Row 0 Column 1 ---- 1.0
Row 1 Column 0 ---- 0.25
Row 1 Column 1 ---- 0.9411764705882353
Row 2 Column 0 ---- 0.5
Row 2 Column 1 ---- 0.8
Row 3 Column 0 ---- 0.75
Row 3 Column 1 ---- 0.64
Row 4 Column 0 ---- 1.0
Row 4 Column 1 ---- 0.5

Thanks

This question has already been answered. Start a new discussion instead.