Trying to extend AbstractList

Any help is appreciated. I am unsuccessfully trying to extend an AbstractList. I have looked at Java documents regarding [url=http://java.sun.com/javase/6/docs/api/java/util/AbstractList.html]AbstractList[/url] as well as the skimpy [url=http://java.sun.com/docs/books/tutorial/collections/custom-implementations/index.html]Sun Custom Collection Tutorial[/url], but I'm still drawing blank.

A couple of basic questions: Is my internal representation of the array a standard Array? something like myJ below? And if so, since I want to make this a variable sized collection, the docs state that I must override the add and remove methods. If my internal representation is a nonvariable sized array, how do I go about doing this? Or am I way off base to begin with?

import java.util.AbstractList;

publicclass JunkListextends AbstractList<Junk>

{

privatefinal Junk[] myJ;

public JunkList()

{

myJ =new Junk[0];

}

@Override

public Junk get(int index)

{

return myJ[index];

}

@Override

public Junk set(int index, Junk ele)

{

Junk oldValue = myJ[index];

myJ[index] = ele;

return oldValue;

}

@Override

publicint size()

{

return myJ.length;

}

}

Many thanks in advance!

/Pete

[2178 byte] By [petes1234a] at [2007-11-27 6:56:07]
# 1

You can use an array to store your elements. That's what ArrayList does. When the number of elements becomes larger than your array size, you create a new, larger array and copy the elements from the old one, then add the new element at the end.

You can look at the source for java.util.ArrayList. It's in src.zip in the JDK download.

jverda at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 2

> A couple of basic questions: Is my internal

> representation of the array a standard Array?

Not that there is any other kind of array...

> something like myJ below?

Yes, exactly.

> And if so, since I want

> to make this a variable sized collection, the docs

> state that I must override the add and remove

> methods. If my internal representation is a

> nonvariable sized array, how do I go about doing

> this? Or am I way off base to begin with?

Well, you will have to create a new array and copy the stuff over using System.arrayCopy.

The question is: is it necessary for your list to be array-based? There might be other means.

CeciNEstPasUnProgrammeura at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 3

If you really want to wrap the array into an AbstractList then that should be alright.

From http://java.sun.com/javase/6/docs/api/java/util/AbstractList.html

To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for the get(int) and size() methods.

khandes3a at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 4
Not quite sure what it is you mean with "standard Array", but only the size() (from AbstractCollection) and get(...) method are abstract and thus need to be overridden when extending the AbstractList class.
prometheuzza at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 5

> Not quite sure what it is you mean with "standard

> Array", but only the size() (from

> AbstractCollection) and get(...) method are

> abstract and thus need to be overridden when

> extending the AbstractList class.

However, note that, for example, AbstractList's add implementation throws UnsupportedOperationException. There's no way the abstract class could provide a functioning add method without knowing what the backing store is.

jverda at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 6

@jverd: That is exactly what I need. I never knew that I had the java source on my hard drive (and I kind of feel stupid about this). ArrayList was easy to find in the util directory, and it contains pretty much all I need to know.

@CeciNest...etc..: Thanks for the useful info. No, it is not necessary for the list to be array-based, but it would be convenient for now. But mainly, I just wanted to know how to extend the AbstractList class. My Junk class truly has junk and nothing else.

@Khandes3: If you look at my original post, I already had a link to the sun AbstractList docs. This info was already known to me, but thanks anyway.

@prometheuzz: I mean standard array as in Junk[] rather than an ArrayList<Junk>.

Again, thanks!

petes1234a at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> I am unsuccessfully trying to extend an AbstractList

Well, we don't know why you are doing this so its hard to make a suggestion.

Whats wrong with an ArrayList or Vector?

You can always look at the source code of those classes to see what they do.

> Is my internal representation of the array a standard Array?

It can be anything you want. Presumably you are doing this because you don't like something about ArrayList or Vector so you can use any internal implemtation that you want.

> And if so, since I want to make this a variable sized collection,

Which is what ArrayList and Vector do.

> If my internal representation is a nonvariable sized array, how do I go about doing this?

Again you can look at the source code. It would look after the "growing" of the internal representation, probably by reallocating the nonvariable size array to a a larger size and then copy all the data, which is done by the ArrayList and Vector classes.

camickra at 2007-7-12 18:32:57 > top of Java-index,Java Essentials,Java Programming...