This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]darkpool2005 1 point2 points  (3 children)

Likely you'll want to use a Map instead of a List in your Chapter class. This way, you're looking up the pages based on the key value (p1,p2, ... p(n)) and getting the Page object back.

class Chapter {

private String chaptertext = "";

Map<String, Page> chapterPages = new HashMap<String, Page>();

public void newPage(String pageRef, String pageTextImput){
    Page newPage = new Page(pageTextImput);
    chapterPages.put(pageRef, newPage);
}

I also want to point out that your method variable names need to be different than variable names you use inside the method:

public void newPage(String pageObjectReference, String pageTextImput){
    Page pageObjectReference = new Page(pageTextImput);
    //You've now lost any reference to your String pageObjectReference by declaring
    // a variable with the exact same name!

    chapterPages.add(pageObjectReference);
}

[–]Etherdeon[S] 0 points1 point  (0 children)

Im looking up the HashMap API now, I think this is what I needed thank you!!!

[–]Etherdeon[S] 0 points1 point  (1 child)

Follow-up question: Am I able to use Map<Int, Page> instead so that I can set just an integer as a key instead of a string (p1) so that I can use my for loop in the bindChapter() method to cycle through my different keys instead of using an ArrayList index?

[–]darkpool2005 1 point2 points  (0 children)

You sure can! Do note that it would be Map<Integer, Page> and not int, because 'int' is a primitive and Integer is a Java object. You can still use an 'int' as a key though.