Lisp-style list

H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hi,

I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)
when for some value < i no value is present yet, these values would just
all be null. Is there something like this, or will I have to do it the
hard way with an ArrayList?

Thanks, H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD+uz3e+7xMGD3itQRAlRkAJ0anEsfddzmZSrAczYj6hGh4flaAACfXLYa
Hu0dZZ374pTwItG2CXYfpYo=
=L196
-----END PGP SIGNATURE-----
 
W

Wibble

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hi,

I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)
when for some value < i no value is present yet, these values would just
all be null. Is there something like this, or will I have to do it the
hard way with an ArrayList?

Thanks, H.
It sounds to me like ArrayList is the easy way.

Lisp lists dont directly support most of what
you want, functions do. Its easy enough
to write a simple class implementing a cons
cell with a next pointer, and add whatever
'lisp' methods to that, but if all you need
is a list, go with the provided ones.
 
R

Robert Klemme

Hendrik Maryns said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Hi,

I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)
when for some value < i no value is present yet, these values would just
all be null. Is there something like this, or will I have to do it the
hard way with an ArrayList?

AFAIK you'll have to do it your own - unless you find a lib out there that
does it. However, you can inherit AbstractList and get a lot methods for
free.

Alternatively you could use a map with int as index, but unless you are on
1.5 you'll have to do the boxing yourself. Note also, that boxing of
primitive values has some performance implications depending on your
application.

Kind regards

robert
 
S

Stefan Ram

Hendrik Maryns said:
I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)

This has nothing to do with Lisp. Something similar is called
a "sparse array". You could use a Map implementation in
Java for this purpose, for example, "java.util.HashMap".

I you really need Lisp, there are Java-libraries for it, like
»Jatha«.

A Lisp list is easily done:

private final int car; private final List cdr;
List( final int car, final List cdr ){ this.car = car; this.cdr = cdr; }

But this is not what you asked for.
 
R

Roedy Green

I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)
when for some value < i no value is present yet, these values would just
all be null. Is there something like this, or will I have to do it the
hard way with an ArrayList?

see http://mindprod.com/jgloss/arraylist.html#GROWINGARRAYLIST
 
R

Roedy Green

I want a list in which I can add at any place, but whose size is not
statically known. So after creating, if I do get(i) for an i for which
there is no value yet, I?d just get null, and if I do add(i, element)
when for some value < i no value is present yet, these values would just
all be null. Is there something like this, or will I have to do it the
hard way with an ArrayList?

see also http://mindprod.com/products1.html#LINKEDLIST

I wrote it for Java 1.0. It is implemented as a chain of objects, no
backing array like Sun's later version.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Roedy Green schreef:

Thanks Roedy, that is exactly what I need, with some more methods like
this for get and add, but I can add them myself. I looked at the page
before, but have read over it.

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD/DcUe+7xMGD3itQRAq+wAJ4xEmICNnlb5oOPSCobemyiSEAR2ACfYbxH
l9Kgf0okcyB3NxQcj+1/NHo=
=ArCx
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

Roedy Green schreef:
see also http://mindprod.com/products1.html#LINKEDLIST

I wrote it for Java 1.0. It is implemented as a chain of objects, no
backing array like Sun's later version.

You must have meant http://mindprod.com/jgloss/linkedlist.html.

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD/Ddie+7xMGD3itQRAu3hAJ9IU3NIR8/ox7XIeKQSD3o7yfusMACdHQqA
jhYFY2upWixRJp+Bvb2QueI=
=FfRV
-----END PGP SIGNATURE-----
 
R

Roedy Green

Thanks Roedy, that is exactly what I need, with some more methods like
this for get and add, but I can add them myself. I looked at the page
before, but have read over it.

Inherited get is ok, unless you want it to grow the array rather than
throw a subscript out of range.

inherited add should do fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top