• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance when extending ArrayList of superclass? Help? :[  RSS feed

 
Xiao Tuzang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. This has been driving me crazy for a few days now.

I have a homework assignment that involves making an index creating program. Basically, the program reads a text file and outputs another text file as an "index" of all the words and their line numbers from the input file.

One class is called "IndexEntry". Every instance of this class represents one line on the output file, essentially holding a String of the word and its line numbers. Here's its code:



I commented some stuff out in the add(int num) method because it seems that an ArrayList can in fact take ints before turning them into objects and storing them.


Next is the "DocumentIndex" class. It extends an ArrayList of IndexEntry and represents all of the IndexEntry instances (I think). Here is its code:



As you can see, there isn't much there yet.

There's also a third class with the main method and some other important stuff, but I don't think it pertains to my problem and so I won't post it unless requested.


Here are my problems:

1) I don't really understand how "DocumentIndex" can extend an ArrayList of "IndexEntry"s. What does this mean? What is inherited?

2) "DocumentIndex" needs to look at every instance of "IndexEntry" to see if a word already exists in an entry before inserting it into a new IndexEntry (which is what "DocumentIndex"'s add method is for). How do I do this? I thought about using a for-each loop, something we just learned in my programming class, but no luck.


I don't think my problem is too complicated but I'm a total novice and was just introduced to ArrayLists and Arrays last week. I've done some Google searching and should've gone to bed awhile ago but I can't find any answers. If you can help me, thank you.
 
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
Xiao Tuzang wrote:
1) I don't really understand how "DocumentIndex" can extend an ArrayList of "IndexEntry"s. What does this mean? What is inherited?



Basically, the DocumentIndex class is subclass of the ArrayList class. However, the ArrayList class supports generic types -- and the DocumentIndex class does not. It is inheriting from the ArrayList class with a specific generic type -- specifically the IndexEntry type.

Henry
 
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
Xiao Tuzang wrote:
I don't think my problem is too complicated but I'm a total novice and was just introduced to ArrayLists and Arrays last week. I've done some Google searching and should've gone to bed awhile ago but I can't find any answers. If you can help me, thank you.


Did you learn Generics yet?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!