[1511] in java-interest

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

Re: dynamic lists

daemon@ATHENA.MIT.EDU (Jason Weiler)
Wed Sep 6 16:27:25 1995

Date: Wed, 6 Sep 1995 10:49:35 -0700
From: weilerj@std.teradyne.com (Jason Weiler)
To: java-interest@java.Eng.Sun.COM


> I am trying to implement something on the level of a linked list of 
> indeterminate length in java.  From reading the docs and hearing a few
> talks, it seems that the Array is the data structure for this.
> But doesn't the array need a size at time of creation?
> Is there a way to "grow" an array (sort of a realloc)?
> 
> I want to go along collecting Strings and adding them to a list of some
> kind: I won't know how many elements will be in the list till I'm done 
> collecting.
> 
> Once I figure this out, I'll be passing the contents of this list back
> from a native method that does a database lookup to retrieve the
> data, then turn it into Strings: java will then do the 
> display stuff.
> 

Just cuz Java can't do pointers, doesn't mean you can't do linked 
lists the old fashioned way. (Now if I'm wrong here, someone please
snap me back in line. :-)

I wrote this cheesy little class on a lark to see if it could be done,
which it obviously could.

********************* start code ********************* 

import java.lang.*;
 
class ll {
 
        static private ll  head=null;
        static private ll point=null;
        private int value;
        private ll next=null;
 
        protected ll (int newval) {
                value = newval;
                next = null;
                }
 
        public ll () {
                value = 0;
                next = null;
                }
        public void addlink(int newval) {
                if (head == null) {
                        head = new ll(newval);
                        }
                else {
                        point = head;
                        while (point.next != null)
                                point = point.next;
                        point.next = new ll(newval);
                        }
                }
        public void show() {
                if (head==null) {
                        System.out.println("Sorry, head is null");
                        }
                else {
                        point = head;
                        while (point != null) {
                                System.out.println(point.value);
                                point = point.next;
                                }
                }
        }
 
        public static void main (String args[]) {
                ll top = new ll();
                top.addlink(0);
                top.addlink(1);
                top.addlink(5);
                top.addlink(3);
                top.show();
                top.show();
        }
}

********************* stop code ********************* 

Well, that should just about do it.  I know it's not commented, but
as I said, it was a test program to begin with, and it's very simple.

If anyone needs help with it, please drop me a line.

-Jason W.
<weilerj@std.teradyne.com>

-
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