6 Replies Latest reply: Jan 5, 2005 12:28 AM by 807596 RSS

    (didn't think I was new.. ) reference question

    807596
      Looking at/modifying a data structures exercise on the web, trying to get my mind around this. Prints out:
      k= 1: p =1, 2; result= 1, 2
      k= 2: p =2, 3; result= 1, 2, 3
      k= 3: p =3, 4; result= 1, 2, 3, 4

      1, 2, 3, 4

      why does "result" hold the whole list? It 's value is assigned to "p" in the initializer of the for loop, but it does not equal "p" in the body of the for loop. Thanks for any help.
      public class IntList {
        public int head;
        public IntList tail;
      
        /** A List with head and tail. */
        public IntList (int head, IntList tail) {
          this.head = head; this.tail = tail;
        }
         /** A new IntList containing the ints in ARGS. */
        public static IntList list (Integer ... args) {
          IntList result, p;
      
          if (args.length > 0)
            result = new IntList (args[0], null);
          else 
            return null;
      
          int k;
          for (k = 1, p = result; k < args.length; k += 1, p = p.tail)
            p.tail = new IntList (args[k], null);
          return result;
        }
      
        
        static void printList(IntList list){
           System.out.print(list.head);
           if (list.tail != null) System.out.print(", ");
                if(list.tail == null)
                     return;
                else{
                     list= list.tail;
                     printList(list);
                }
      }
      
      public static IntList ListMaker(Integer ...args){
           IntList p, result;
           int k;
           
          if (args.length > 0)
              result = new IntList (args[0], null);
            else 
              return null;
      
           p= new IntList(args[0], null);
           for(k=1, p= result; k<args.length; k++, p= p.tail){
                p.tail = new IntList(args[k], null);
                System.out.print("k= "+k+": ");
                System.out.print("p =");
                printList(p);
                System.out.print(";  result= ");
                printList(result);
                System.out.println();
           }
           System.out.println();
           return result;
      }
      
                
           public static void main(String[]args){
                printList(ListMaker(1,2,3,4));
           }
      
      }
        • 1. Re: (didn't think I was new.. ) reference question
          807596
          Given p = result, until another assignment is made, p == result (or p is the exact same Object as result). But p is reassigned, at the end of that for-loop (in the for-loop's third clause) to p.tail.
          • 2. Re: (didn't think I was new.. ) reference question
            807596
            Yes, but result is an IntList, which has an int head and an IntList tail. The first time in the body of the loop, the head is "1" (assigned to p before entering the loop), and the tail of {2,null} is assigned to p and so is assigned to result. "p" is {1,{2,null}}, and so is "result" as well. So far so good.

            Then at the beginning of the next iteration (or the end of the first), p is assigned as p.tail, so p now = {2,null}. but at the top of the second iteration, result = 1,2. why does p change but not result? Also, at the end of it all, result = {1,{2,3,4}} How did the tail grow?
            here's a more detailed output:
            Top of loop: result= 1; p= 1
            k=1: p= 1, 2; at bottom, result= 1, 2

            Top of loop: result= 1, 2; p= 2
            k=2: p= 2, 3; at bottom, result= 1, 2, 3

            Top of loop: result= 1, 2, 3; p= 3
            k=3: p= 3, 4; at bottom, result= 1, 2, 3, 4

            1, 2, 3, 4
            • 3. reformatted; please I am really puzzled
              807596
              Why does "result" change value even though it never receives an assignment after the for loop is entered?
              public class IntList {
                public int head;
                public IntList tail;
              
                /** A List with head and tail. */
                public IntList (int head, IntList tail) {
                  this.head = head; this.tail = tail;
                }
              
                public String toString(){
                   System.out.print("{"+this.head);
                   if (this.tail == null){
                        return"}";
                   }
                   IntList list = this.tail;
                   return(list.toString() + "}");
                }
              
                public static IntList listMaker(Integer ...args){
                   IntList p, result;
                   int k;
                   
                  if (args.length > 0)
                      result = new IntList (args[0], null);
                  else 
                      return null;
              
                   for(k=1, p= result; k<args.length; k++, p= p.tail){
                        System.out.print("\nTop of loop: result= ");
                        System.out.print(result);
                        System.out.print(";  p= ");
                        System.out.print(p);
                        System.out.println();
                        p.tail = new IntList(args[k], null);
                        System.out.print("k="+k+": ");
                        System.out.print("p= ");
                        System.out.print(p);
                        System.out.print("; at bottom, result= ");
                        System.out.print(result);
                        System.out.println();
                   }
                   System.out.println();
                   return result;
              }
              
                        
                   public static void main(String[]args){
                        System.out.print(ListMaker(1,2,3,4));
                   }
              
              }
              output:

              Top of loop: result= {1}; p= {1}
              k=1: p= {1{2}}; at bottom, result= {1{2}}

              Top of loop: result= {1{2}}; p= {2}
              k=2: p= {2{3}}; at bottom, result= {1{2{3}}}

              Top of loop: result= {1{2{3}}}; p= {3}
              k=3: p= {3{4}}; at bottom, result= {1{2{3{4}}}}

              {1{2{3{4}}}}
              • 4. Re: reformatted; please I am really puzzled
                796440
                Why does "result" change value even though it never
                receives an assignment after the for loop is
                entered?
                Because p and result and every other variables that's not a primitive (like int, float, boolean, etc.) is a reference, not an object, and assigning p=result just copies that reference, not the object it points to.

                At the start of the for loop, result holds the address of an IntList. In the loop initializer, you copy that addess into p. Now p and result both point at the same IntList. Any changes you make to the state (the "contents") of that IntList with p.whatever = or result.whatever = will be visible through both p and result, because there's only one IntList and they both refer to it.

                On the other hand, any changes you make to those reference variables, through p = or result = will only affect that variable--not the IntList object and not any other variable that refers to it.
                • 5. Re: reformatted; please I am really puzzled
                  807596
                  I think I got it by staring, though an explanation in someone else's words would help me too.

                  In the first iteration of the loop, result is a reference to a list with a null tail. In the body of the loop that tail is assigned a value of a newIntList(), a list which happens to have a null tail.

                  In teh increment portion of the for loop, the reference to that tail object is assigned to the variable p. In the next iteration, that tail object (which itself has a null tail) is assigned a tail of its own, etc.

                  The "result" reference refers to the original IntList object that had a tail that was originally null, but subsequently that tail received a non-null assignment (and its tail after it....).

                  Thanks for listening.
                  • 6. Re: reformatted; please I am really puzzled
                    807596
                    thanks jverd