• Post Reply Bookmark Topic Watch Topic
  • New Topic

create my own arraylist implementing Collection<T> without implementing arraylist itself  RSS feed

 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm trying to create my own arraylist using Collection. My program doesn't do anything, despite 16 hours of sitting here. Obviously, I haven't a clue. Can somebody point me in the right direction? I appreciate any help.

 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Surely you should be implementing List<T> rather than Collection<T>?
It looks as though you are trying to do too much all at once. How do you intend to design such a List? Have you got a design on paper? If you don't have a design, you will get no end of confusion.
Have you decided what size represents? Are you confusing size and capacity?
I suggest you write all the methods in the List interface, but in each of them you do what NetBeans would:-That will prevent you missing out any un‑implemented methods.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that constructor will do you any good; you are not initialising the array, and why are you taking a String[] as its parameter.
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to implement Collection. It's my assignment. This code (or variation of) will work by implementing arraylist. I tried it out, yet we are not allowed to do that. I know my object isn't initialized, however, I'm not sure how to do it. Every time I try, it throws an error.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:Hello. I'm trying to create my own arraylist using Collection. My program doesn't do anything, despite 16 hours of sitting here. Obviously, I haven't a clue. Can somebody point me in the right direction?...

Yes:

1. StopCoding (←click).
2. You're missing several methods; not the least of which is iterator().
3. Unless you really want to do this completely from scratch, extend AbstractCollection. The class was written for precisely what you want to do.
4. An even better choice, since you want to emulate ArrayList, is to extend AbstractList (which IS a Collection).
5. If you absolutely feel you must do this completely from scratch, you've taken on quite a task; and I suspect it's overwhelming you. So: deal with one method at a time. Write it, and test the hell out of it before you write anything else. When you have fully implemented (and tested) ALL required methods, then add the 'implements Collection<T>'.

HIH

Winston
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to StopCoding. First decide how exactly is your class going to store data. Do it on paper first. Or sometimes, I like to play a game where I pretend to be the class I am going to implement. This helps you get into the "head" of your class. Once you see how the class is suppossed to behave, implementing it becomes easy. Be one with the class. Be the class.

In this case, the way I would pretend to be the class by using paper cups. Take a stack of paper cups and lay 5 of them down in a line. This is your initial empty array. You have began your new life as an ArrayList with just 4 empty paper cups. From now on, nothing exists but you and the paper cups. Now pretend someone gives you a banana and tells you to add it to the list. You have the banana. What do you do with it? No, don;t put it there!!. Use the paper cups.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:5. If you ... must do this completely from scratch...

Another alternative is to fully implement the interface with methods that ALL simply throw UnsupportedOperationException to start with.

Then work as suggested above: implement ONE method, test it until you're blue in the face, and don't move on until you KNOW it works.

Winston
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have to do it from scratch. I'm not missing iterator, it's just under the rest of the code that I threw an UnsupportedOperationException on. I still don't know where to begin.
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Winston Gutkowski wrote:5. If you ... must do this completely from scratch...

Another alternative is to fully implement the interface with methods that ALL simply throw UnsupportedOperationException to start with.

Then work as suggested above: implement ONE method, test it until you're blue in the face, and don't move on until you KNOW it works.

Winston


I did fully implement the interface. I just don't know how to make it work.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:I did fully implement the interface. I just don't know how to make it work.

No you didn't. As I already said, you haven't implemented iterator() (at least not in what you posted), which is the main reason to have a Collection.

I say again: You're overwhelmed because you're trying to do too much at once. Back off (and follow Jayesh's advice) and deal with one method at a time.

Winston
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Heather Walsh wrote:I did fully implement the interface. I just don't know how to make it work.

No you didn't. As I already said, you haven't implemented iterator() (at least not in what you posted), which is the main reason to have a Collection.

I say again: You're overwhelmed because you're trying to do too much at once. Back off (and follow Jayesh's advice) and deal with one method at a time.

Winston


Iterator is there, I just didn't put it up. I don't even know how to get one method to work.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:I think I have to do it from scratch. I'm not missing iterator, it's just under the rest of the code that I threw an UnsupportedOperationException on. I still don't know where to begin.

Then plainly you're working from something different to what you posted to us, because I don't see any sign of it - nor indeed any method that throws a UOE.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:Iterator is there, I just didn't put it up.

Well, that doesn't help us much.

I don't even know how to get one method to work.

Because (almost certainly) you're overthinking this. Do what I suggested, and start with something simple, like size(). How do you think you might implement that?

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:
Iterator is there, I just didn't put it up. I don't even know how to get one method to work.


Creating method stubs isn't implementing them -- it is just done so that you can get the compiler to stop complaining, so that you can work on implementing them.

And as you can tell, depending on how you created the stubs, it may not be possible to implement them.


Anyway, as a first recommendation (since you are obviously not going to take the StopCoding recommendation), is to change your mArray instance variable to a type of Object array. Since it is not possible to create an array of type T in the class, you won't be able to initialize it (without external help) or even resize it when needed.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Anyway, as a first recommendation (since you are obviously not going to take the StopCoding recommendation), is to change your mArray instance variable to a type of Object array. Since it is not possible to create an array of type T in the class, you won't be able to initialize it (without external help) or even resize it when needed.

A very good point. The only thing you will need to do in that case is to cast an element whenever you need to return it, viz:
return (T) mArray[whatever];

Winston
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Heather Walsh wrote:Iterator is there, I just didn't put it up.

Well, that doesn't help us much.

I don't even know how to get one method to work.

Because (almost certainly) you're overthinking this. Do what I suggested, and start with something simple, like size(). How do you think you might implement that?

Winston


My apologies. My eyes are nearly swollen shut from crying. I tried to do size first but got error after error.

I have:
private static int mSize;
@Override
public int size(){
return animals.size();
}
in my public static void:
System.out.println("Size: " + mSize);

it prints out:
Size: 0
which is not the size of the list, which should be 3.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Henry Wong wrote:Anyway, as a first recommendation (since you are obviously not going to take the StopCoding recommendation), is to change your mArray instance variable to a type of Object array. Since it is not possible to create an array of type T in the class, you won't be able to initialize it (without external help) or even resize it when needed.

A very good point. The only thing you will need to do in that case is to cast an element whenever you need to return it, viz:
return (T) mArray[whatever];


Or - and this is the way I'd do it - you can leave it declared as a T[], and cast on creation:
It works, trust me . The cast is enough for the compiler, and type erasure means it works at runtime.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heather Walsh wrote:

My apologies. My eyes are nearly swollen shut from crying.


In which case, you really really want to stop coding and go get some sleep.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Or - and this is the way I'd do it - you can leave it declared as a T[], and cast on creation:
It works, trust me . The cast is enough for the compiler, and type erasure means it works at runtime.

Very true, but it can lull you into a false sense of security because it's not assignable. It works if, and only if, elements are properly typed (which would hopefully be the case), but you can run into runtime issues if you don't remember that arrays aren't reifable.

@Heather: This is slightly more advanced stuff, so don't worry about it at the moment. Matthew's suggestion will work just fine (and is actually a lot tidier) providing you use proper generic types on all your methods.

I'm with Jayesh: Get some sleep. And remember: this is a program, not the countdown to a nuclear holocaust. Worlds (even yours) won't crumble if you don't get this done by tomorrow morning.

Winston
 
Heather Walsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:
Heather Walsh wrote:

My apologies. My eyes are nearly swollen shut from crying.


In which case, you really really want to stop coding and go get some sleep.


Thank you.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seriously. It doesn't matter how much of an hard ass your professor is. S/he doesn;t want you crying while doing this assignment. You are not going to learn unless you are having fun. Go to sleep and come back when you are ready to have fun
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: . . .Or - and this is the way I'd do it - you can leave it declared as a T[], and cast on creation:
It works, trust me . . . .
Agree.
Slight enhancement I would suggestI suggest you go through the Java Tutorials about annotations to make sure I have got the syntax right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!