// Program Name:                 Array2DTest.java
// Course:                       CSE 1302J
// Student Name:                 Bradley Shedd
// Assignment Number:            Lab#3
// Due Date:                     09/15/2010
// Purpose:    This creates a program that will read in two values, row and col, and create an array of that size.
//                              
// Array2DTest.java
// simple use of two-dimensional arrays

    public class Array2DTest
   {
      
public static void main (String[] args)
      {
        
int [] [] A = new int [2] [5]; // 2 rows, 5 columns
      // fill each cell with the sum of its row and column numbers
         for (int i = 0; i < 2; i++)
         {
           
for (int j = 0; j < 5; j++)
            {
               A[i] [j] = i + j;
            }
         }
        
int [] [] B = {{1,2,3,4,5}, {6,7,8,9,10}};  // 2 rows, 5 columns
         System.out.println ("A.length = " + A.length);
         System.out.println (
"B.length = " + B.length);
         System.out.println (
"Number of columns in A is " + A[0].length);
         System.out.println (
"Number of cells in A is " + A.length * A[0].length);
     
// compute row sums for B
         for (int i = 0; i < B.length; i++)
         {
           
int sum = 0;
           
for (int j = 0; j < B[i].length; j++)
            {
               sum = sum + B[i] [j];
            }
            System.out.println (
"The sum of row B " + i + " is " + sum);
         }
     
// compute column sums for B here
         for (int i=0; i<5; i++)
         {
           
int sum2 = 0;
           
for(int j= 0; j< 2; j++)
            {
               sum2 += B[j][i];
            }
            System.out.println (
"The sum of column B " + i + " is " + sum2);
         }
         System.out.println(
"Coded by: Bradley J. Shedd");
      }
   }

Homepage