Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Box {
- int size;
- Box (int s) {
- size = s;
- }
- }
- public class Laser {
- public static void main(String[] args) {
- Box b1 = new Box(5);
- Box[] ba = go(b1, new Box(6));
- ba[0] = b1;
- for(Box b : ba)
- System.out.println(b.size + " ");
- }
- static Box[] go (Box b1, Box b2) {
- b1.size = 4;
- Box[] ma = {b2, b1};
- return ma;
- }
- }
- The important TWIST that you missed here is;
- public static void main(String[] args) {
- Box b1 = new Box(5); // b1 == location A
- Box[] ba = go(b1, new Box(6)); // ba == location B which stores Location A, D
- // PLEASE NOTE HERE
- // After the function go() is executed;
- // ba[] will have {D, A}
- // So ba[0] will have object b1 which is at location A.
- ba[0] = b1; // location B will now store A and A
- for(Box b : ba)
- System.out.println(b.size + " "); // Output: 4 and 4
- }
- static Box[] go (Box b1, Box b2) { // go(location A, location D )
- b1.size = 4; // A's object.size = 4
- Box[] ma = {b2, b1}; // position is interchanged here (D and A)
- return ma; // return the location of ma
- }
- Box** go (Box *b1, Box *b2) {
- b1->size = 4;
- /* allocate an array of Box*, set it up, return it */
- }
- b1 = &some_other_box;
- public static void main(String[] args) {
- Box b1 = new Box(5);
- Change(b1);
- System.out.println(b1.size + " ");
- static void Change(Box b1) {
- b1 = new Box(6);
- }
- public static void main(String[] args) {
- Box b1 = new Box(5);
- Change(b1);
- System.out.println(b1.size + " ");
- static void Change(Box b1) {
- b1.size = 6;
- }
- public class Laser {
- public static void main(String[] args) {
- //Create a Box of size 5.
- Box b1 = new Box(5);
- //Create an array of Box objects, which are the results of the go method
- Box[] ba = go(b1, new Box(6));
- // ba now looks like {new Box(6), b1 /*b1 is size 4 now*/}
- // assign reference to b1 at index 0 in ba.
- ba[0] = b1;
- // array now looks like {b1, b1}
- for(Box b : ba)
- System.out.println(b.size + " ");
- }
- static Box[] go (Box b1, Box b2) {
- //set the size of the first box to 4
- b1.size = 4;
- //create an array of boxes, with b2 in index 0 and b1 in index 1
- Box[] ma = {b2, b1};
- return ma;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement