Like an array of a primitive type, a multidimensional array can be made a field of a class. You can primarily declare it without initializing it. Here is an example: public class TriangleInCoordinateSystem { private int[][] points; } This indicates a two-dimensional array field: the array will contain two lists but we don't know (yet) how many members each array will contain. Therefore, if you want, when creating the array, you can specify its dimension by assigning it a second pair of square brackets using the new operator and the data type. Here is an example: public class TriangleInCoordinateSystem { private int[][] points = new int[3][2]; } You can also use a method of the class or a constructor to indicate the size of the array. Here is an example: public class TriangleInCoordinateSystem { private int[][] points; public TriangleInCoordinateSystem() { points = new int[3][2]; } } To initialize the array, you can access each member using the square brackets as we saw in the previous sections. Here is an example: public class TriangleInCoordinateSystem { private int[][] Points; public TriangleInCoordinateSystem() { Points = new int[3][2]; points[0][0] = -2; // A(x, ) points[0][1] = -3; // A( , y) points[1][0] = 5; // B(x, ) points[1][1] = 1; // B( , y) points[2][0] = 4; // C(x, ) points[2][1] = -2; // C( , y) } } After initializing the array, you can use it as you see fit. For example, you can display its values to the user. Here is an example: class TriangleInCoordinateSystem { private int[][] points; public TriangleInCoordinateSystem() { points = new int[3][2]; points[0][0] = -2; // A(x, ) points[0][1] = -3; // A( , y) points[1][0] = 5; // B(x, ) points[1][1] = 1; // B( , y) points[2][0] = 4; // C(x, ) points[2][1] = -2; // C( , y) } public void showPoints() { System.out.println("Coordinates of the Triangle"); System.out.printf("A(%d, %d)\n", points[0][0], points[0][1]); System.out.printf("B(%d, %d)\n", points[1][0], points[1][1]); System.out.printf("C(%d, %d)", points[2][0], points[2][1]); } } public class Exercise { public static void main(String[] args) throws Exception { TriangleInCoordinateSystem triangle = new TriangleInCoordinateSystem(); triangle.showPoints(); } } This would produce: Coordinates of the Triangle A(-2, -3) B(5, 1) C(4, -2)
A multidimensional array can be passed as argument. When creating the method, in its parentheses, enter the data type followed by the necessary number of combinations of square brackets. Here is an example: public class TriangleInCoordinateSystem { public void showPoints(int[][] Coords) { } } When defining the method, in its body, you can use the array as you see fit, such as displaying its values. Here is an example: public class TriangleInCoordinateSystem { public void showPoints(int[][] pts) { System.out.println("Coordinates of the Triangle"); System.out.printf("A(%d, %d)\n", pts[0][0], pts[0][1]); System.out.printf("B(%d, %d)\n", pts[1][0], pts[1][1]); System.out.printf("C(%d, %d)", pts[2][0], pts[2][1]); } } To call this type of method, pass only the name of the array. Here is an example: class TriangleInCoordinateSystem { public TriangleInCoordinateSystem() { } public void showPoints(int[][] pts) { System.out.println("Coordinates of the Triangle"); System.out.printf("A(%d, %d)\n", pts[0][0], pts[0][1]); System.out.printf("B(%d, %d)\n", pts[1][0], pts[1][1]); System.out.printf("C(%d, %d)", pts[2][0], pts[2][1]); } } public class Exercise { public static void main(String[] args) throws Exception { int points[][] = new int[3][2]; points[0][0] = -2; // A(x, ) points[0][1] = -3; // A( , y) points[1][0] = 5; // B(x, ) points[1][1] = 1; // B( , y) points[2][0] = 4; // C(x, ) points[2][1] = -2; // C( , y) TriangleInCoordinateSystem triangle = new TriangleInCoordinateSystem(); triangle.showPoints(points); } } As indicated with one-dimensional arrays, when passing an multi-dimensional array as argument, the array is treated as a reference. This makes it possible for the method to modify the array and return it changed. Here is an example: class TriangleInCoordinateSystem { public void createPoints(int[][] coords) { coords[0][0] = -2; // A(x, ) coords[0][1] = -3; // A( , y) coords[1][0] = 5; // B(x, ) coords[1][1] = 1; // B( , y) coords[2][0] = 4; // C(x, ) coords[2][1] = -2; // C( , y) } public void showPoints(int[][] pts) { System.out.println("Coordinates of the Triangle"); System.out.printf("A(%d, %d)\n", pts[0][0], pts[0][1]); System.out.printf("B(%d, %d)\n", pts[1][0], pts[1][1]); System.out.printf("C(%d, %d)", pts[2][0], pts[2][1]); } } public class Exercise { public static void main(String[] args) throws Exception { int points[][] = new int[3][2]; TriangleInCoordinateSystem triangle = new TriangleInCoordinateSystem(); triangle.createPoints(points); triangle.showPoints(points); } }
You can return a multi-dimensional array from a method. When creating the method, before its name, specify the data type followed by the multidimensional combination of square brackets. Here is an example: public class TriangleInCoordinateSystem { public int[][] CreateTriangle() { } } In the body of the method, you can do what you want but, before exiting it, you must return an array of the same type that was created. Here is an example: class TriangleInCoordinateSystem { public int[][] CreateTriangle() { int[][] values = new int[3][2]; return values; } } When calling the method, you can assign it to an array of the same type it returns. Here is an example: class TriangleInCoordinateSystem { public int[][] createTriangle() { int[][] values = new int[3][2]; values[0][0] = 6; // A(x, ) values[0][1] = 1; // A( , y) values[1][0] = 2; // B(x, ) values[1][1] = 3; // B( , y) values[2][0] = 1; // C(x, ) values[2][1] = 4; // C( , y) return values; } public void showPoints(int[][] pts) { System.out.println("Coordinates of the Triangle"); System.out.printf("A(%d, %d)\n", pts[0][0], pts[0][1]); System.out.printf("B(%d, %d)\n", pts[1][0], pts[1][1]); System.out.printf("C(%d, %d)", pts[2][0], pts[2][1]); } } public class Exercise { public static void main(String[] args) throws Exception { int[][] coordinates = new int[3][2]; TriangleInCoordinateSystem triangle = new TriangleInCoordinateSystem(); coordinates = triangle.createTriangle(); triangle.showPoints(coordinates); } } This would produce: Coordinates of the Triangle A(6, 1) B(2, 3) C(1, 4)
As done for primitive data types, you can create a multi-dimensional array where each component is of a class type. Of course, you can use an existing class or you must first create a class. Here is an example: public class Point { private int xCoord; private int yCoord; public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } To create a multidimensional array of objects without initializing it, you can use the following formula: ClassName[][] VariableName; If you know the number of instances of the class that the array will use, you can use the following formula: ClassName[][] VariableName = new ClassName[Number1][Number2]; Remember that you can position the square brackets after the name of the variable: ClassName VariableName[][] = new ClassName[Number1][Number2]; The ClassName factor is the name of the class that will make up each component of the array. The other sections follow the same rules we reviewed for the primitive types. For example, you can create an array without allocating memory for it as follows: class Point { private int xCoord; private int yCoord; public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } public class Exercise { public static void main(String[] args) throws Exception { Point[][] line; } } If you know the number of components that the array will contain, you can use the right pair of square brackets, in which case you can specify the name of the class and the square brackets. Here is an example: public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[2][2]; } } This declaration creates a two-dimensional array of two Point objects: The array contains two lists and each list contains two Points. To initialize a multidimensional array of objects, you can access each array member using its index, allocate memory for it using the new operator. After allocating memory for the member, you can then access its fields or methods to initialize it. Here is an example: class Point { private int xCoord; private int yCoord; public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[2][2]; line[0][0] = new Point(); // First Point A line[0][0].setX(-3); // A(x, ) line[0][0].setY(8); // A( , y) line[0][1] = new Point(); // Second Point B line[0][1].setX(4); // B(x, ) line[0][1].setY(-5); // B( , y) } } You can also initialize the array when creating it. Before doing this, you would need a constructor of the class and the constructor must take the argument(s) that would be used to initialize each member of the array. To actually initialize the array, you would need a pair of external curly brackets for the main array. Inside of the external curly brackets, create a pair of curly brackets for each sub-dimension of the array. Inside the last curly brackets, use the new operator to access an instance of the class and call its constructor to specify the values of the instance of the class. Here is an example: class Point { private int xCoord; private int yCoord; public Point(int x, int y) { xCoord = x; yCoord = y; } public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[][] { { new Point(-3, 8), new Point(4, -5) } }; } }
To access the members of a multidimensional array of objects, apply the square brackets to a particular member of the array and access the desired field or method of the class. Here is an example: public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[][] { { new Point(-3, 8), new Point(4, -5) } }; System.out.println("Line =-="); System.out.printf("From A(%d, %d) to B(%d, %d)", line[0][0].getX(), line[0][0].getY(), line[0][1].getX(), line[0][1].getY()); } } This would produce: Line =-= From A(-3, 8) to B(4, -5) You can also use a loop to access the members of the array. To do this, create a first for loop that stops at the first dimension of the array - 1. Then, inside of a first for loop, nest a for loop for each subsequent dimension. Here is an example: public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[][] { { new Point(-3, 8), new Point(4, -5) } }; System.out.println("The points of the line are:"); for(int i = 0; i < 1; i++) for(int j = 0; j < 2; j++) System.out.printf("(%d, %d)\n", line[i][j].getX(), line[i][j].getY()); } } This would produce: The points of the line are: (-3, 8) (4, -5) To apply a for each operator, access only each member of the internal list. Here is an example: public class Exercise { public static void main(String[] args) throws Exception { Point[][] line = new Point[][] { { new Point(-3, 8), new Point(4, -5) } }; System.out.println("The points of the line are:"); for(Point pts : line[0]) System.out.printf("(%d, %d)\n", pts.getX(), pts.getY()); } }
As done for primitive types, a multidimensional array of objects can be made a field of a class. You can declare the array without specifying its size. Here is an example: public class Triangle { public Point[][] vertices; } If you know the dimensions that the array will have, you can specify them using the new operator at the same time you are creating the field. Here is an example: public class Triangle { public Point[][] vertices = new Point[3][2]; } This creation signals a multidimensional array of Point objects. It will consist of three lists and each list will contain two Point objects To initialize the array, access each member by its index to allocate memory for it. Once you get the member, you access each one of its fields or properties and initialize it with the desired value. Here is an example: public class Triangle { public Point[][] Vertices = new Point[3][2]; public Triangle() { vertices[0][0] = new Point(); // Point A(x, y) vertices[0][0].setX(-2); // A(x, ) vertices[0][0].setY(-4); // A( , y) vertices[1][0] = new Point(); // Point B(x, y) vertices[1][0].setX(3); // B(x, ) vertices[1][0].setY(5); // B( , y) vertices[2][0] = new Point(); // Point C(x, y) vertices[2][0].setX(6); // C(x, ) vertices[2][0].setY(-2); // C( , y) } } If the class is equipped with the right constructor, you can use it to initialize each member of the array. Once the array is ready, you can access each members using its index and manipulate it. For example you can display its value(s) to the user. Here is an example: class Point { private int xCoord; private int yCoord; public Point() { xCoord = 0; yCoord = 0; } public Point(int x, int y) { xCoord = x; yCoord = y; } public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } class Triangle { public Point[][] vertices = new Point[3][2]; public Triangle() { vertices[0][0] = new Point(); // Point A(x, y) vertices[0][0].setX(-2); // A(x, ) vertices[0][0].setY(-4); // A( , y) vertices[1][0] = new Point(); // Point B(x, y) vertices[1][0].setX(3); // B(x, ) vertices[1][0].setY(5); // B( , y) vertices[2][0] = new Point(); // Point C(x, y) vertices[2][0].setX(6); // C(x, ) vertices[2][0].setY(-2); // C( , y) } public void identify() { System.out.print("Triangle Vertices: "); System.out.printf("A(%d, %d), B(%d, %d), and C(%d, %d)", vertices[0][0].getX(), vertices[0][0].getY(), vertices[1][0].getX(), vertices[1][0].getY(), vertices[2][0].getX(), vertices[2][0].getY()); } } public class Exercise { public static void main(String[] args) throws Exception { Triangle tri = new Triangle(); tri.identify(); } } This would produce: Triangle Vertices: A(-2, -4), B(3, 5), and C(6, -2)
You can pass a multidimensional array of objects as arguments. To do this, in the parentheses of the method, enter the class name followed by the appropriate combination of square brackets. Here is an example: public class Triangle { public void create(Point[][] Points) { } } In the body of the method, use the array as you we have done so far. You can access its members to get to its values. Here are examples: class Point { private int xCoord; private int yCoord; public Point() { xCoord = 0; yCoord = 0; } public Point(int x, int y) { xCoord = x; yCoord = y; } public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } class Triangle { public void create(Point[][] points) { points[0][0] = new Point(); // Point A(x, y) points[0][0].setX(-2); // A(x, ) points[0][0].setY(-4); // A( , y) points[1][0] = new Point(); // Point B(x, y) points[1][0].setX(3); // B(x, ) points[1][0].setY(5); // B( , y) points[2][0] = new Point(); // Point C(x, y) points[2][0].setX(6); // C(x, ) points[2][0].setY(-2); // C( , y) } public void identify(Point[][] coords) { System.out.print("Triangle Vertices: "); System.out.printf("A(%d, %d), B(%d, %d), and C(%d, %d)", coords[0][0].getX(), coords[0][0].getY(), coords[1][0].getX(), coords[1][0].getY(), coords[2][0].getX(), coords[2][0].getY()); } } public class Exercise { public static void main(String[] args) throws Exception { Triangle tri = new Triangle(); Point vertices[][] = new Point[3][2]; tri.create(vertices); tri.identify(vertices); } } Remember that an array passed as argument is in fact passed by reference.
A method can return a multidimensional array of objects. If you are creating the method, before its name, type the name of the class followed by the desired combination of square brackets. Here is an example: public class Triangle { public Point[][] create() { } } After implementing the method, before exiting it, make sure it returns the type of array that it was indicated to produce. Here is an example: class Point { private int xCoord; private int yCoord; public Point() { xCoord = 0; yCoord = 0; } public Point(int x, int y) { xCoord = x; yCoord = y; } public int getX() { return xCoord; } public void setX(int x) { xCoord = x; } public int getY() { return yCoord; } public void setY(int y) { yCoord = y; } } class Triangle { public Point[][] create() { Point[][] points = new Point[3][2]; points[0][0] = new Point(); // Point A(x, y) points[0][0].setX(4); // A(x, ) points[0][0].setY(8); // A( , y) points[1][0] = new Point(); // Point B(x, y) points[1][0].setX(2); // B(x, ) points[1][0].setY(-1); // B( , y) points[2][0] = new Point(); // Point C(x, y) points[2][0].setX(3); // C(x, ) points[2][0].setY(0); // C( , y) return points; } public void identify(Point[][] coords) { System.out.print("Triangle Vertices: "); System.out.printf("A(%d, %d), B(%d, %d), and C(%d, %d)", coords[0][0].getX(), coords[0][0].getY(), coords[1][0].getX(), coords[1][0].getY(), coords[2][0].getX(), coords[2][0].getY()); } } public class Exercise { public static void main(String[] args) throws Exception { Triangle tri = new Triangle(); Point vertices[][] = tri.create(); tri.identify(vertices); } } This would produce: Triangle Vertices: A(4, 8), B(2, -1), and C(3, 0)
|
|
|||||||||||||||||||||||||
|