[1735] in java-interest

home help back first fref pref prev next nref lref last post

Re: pointer in java

daemon@ATHENA.MIT.EDU (Simon Spero)
Fri Sep 15 12:22:02 1995

Date: Thu, 14 Sep 1995 12:53:07 -0700 (PDT)
From: Simon Spero <ses@tipper.oit.unc.edu>
To: Larry Liang <lliang@bellcore.com>
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <199509131450.KAA00581@scherzo.bellcore.com>

On Wed, 13 Sep 1995, Larry Liang wrote:

> Since there is no pointer in java, how do I create a linked list of a
> tree. The handle is more like a reference in C++ which does not seem
> to be able to change the object it's referring to. 

Wrong(ish) - just about everything is a pointer in java. I posted an 
example of a doubly linked list a week or so back, but here's a little 
example for a silly linked list implementation. Static functions are used 
to give the usual non-oo interface.

interface closure {
	public object funcall(object o);
};

class list {
  public Object car;
  public list   cdr;

  public list(Object o , list l) {
	_car = o ; _cdr = l
  }

  public static list cons(Object o, list l) {
	return new list(o,l);
   }
  
  public static list append(list a,list b) {
     if(a == nil) {
	return b;
     } else {
	return cons(a.car,append(a.cdr,b));
     }
  }
  
  public static list reverse(list a) {
	return reverse_2(a,nil);
  }
  public static list reverse_2(list a, list acc) {
     if(a == nil) {
	return acc;
      } else {
	return reverse_2(a.cdr,cons(a.car,acc));
      }
   }
  public static list map(closure f, list a) {
   if(a == nil) { 
	 return nil;
    } else {
	return cons(f.funcall(a.car),a.cdr);
    }
  }
};
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post