public class TwoDArray private int rows private int cols private String myArray public TwoDArrayint numRows int numCols rows numRows cols numCols ID: 242638
Download Presentation The PPT/PDF document "Two Dimensional Arrays" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Two Dimensional ArraysSlide2Slide3Slide4Slide5
public class
TwoDArray
{
private
int
rows;
private
int cols; private String[][] myArray; public TwoDArray(int numRows, int numCols) { rows = numRows; cols = numCols; myArray = new String[rows][cols]; } public void fillArray() { for (int row=0; row<rows; row++) { for (int col=0; col<cols; col++) { myArray[row][col] = row + "," + col; } } } public void printArray() { for (int row=0; row<rows; row++) { for (int col=0; col<cols; col++) { System.out.print("myArray[" + row + "][" + col + "] = " + myArray[row][col] + "\t"); } System.out.println(); } } }Slide6Slide7
public class
TestTwoDimArray
{
private Circle[][]
circleArray
;
private final
int rows = 10; private final int cols = 10; private final int diameter = 40; /** * Creates an empty 2-dimensional Circle array. */ public TestTwoDimArray() { circleArray = new Circle[rows][cols]; } /** * Fill the array with circles. */ public void fillArray() { for(int row = 0; row < rows; row++) { for(int col = 0; col < cols; col++) { circleArray[row][col] = new Circle(diameter, col * diameter, row * diameter); } } }