0

I have implemented an interface by using arrays to create a Citation Index. I am a new programmer and want to learn how to convert my current Array implementation into an ArrayList implementation. Here is my code so far. How would I use ArrayLists in the printCitationIndex() method instead of Arrays?

    import java.util.ArrayList;

    public class IndexArrayList implements IndexInterface{
      ArrayList<Citation> citationIndex = new ArrayList<Citation>();
      private String formatType;
      private int totalNumCitations, numKeywords;
      ArrayList<Keyword> keywordIndex = new ArrayList<Keyword>();


    public void printCitationIndex(){
    // Pre-condition - the index is not empty and has a format type
    //Prints out all the citations in the index formatted according to its format type.  
   //Italicization not required
      if (!this.formatType.equals("")) {
        if (!isEmpty()) {
            for (int i = 0; i < citationIndex.length; i++) {
                if (citationIndex[i] != null) {
                    if (this.formatType.equals("IEEE")) {
                        System.out.println(citationIndex[i].formatIEEE());
                    } else if (this.formatType.equals("APA")) {
                        System.out.println(citationIndex[i].formatAPA());
                    } else if (this.formatType.equals("ACM")) {
                        System.out.println(citationIndex[i].getAuthorsACM());
                    }
                }
            }
        } else {
            System.out.println("The index is empty!");
        }
    } else {
        System.out.println("The index has no format type!");
    }
}

}
1
  • It seems like it is just a question of replacing .length with .size() and [i] with .get(i) if I understand your question correctly :)
    – xpa1492
    Commented Nov 28, 2014 at 3:13

2 Answers 2

1

It's best to use the enhanced for-loop for this (it also works for arrays, so the syntax would have been the same with arrays and with lists, if you had found it earlier)

        for (Citation citation : citationIndex) {
            if (citation != null) {
                if (this.formatType.equals("IEEE")) {
                    System.out.println(citation.formatIEEE());
                } else if (this.formatType.equals("APA")) {
                    System.out.println(citation.formatAPA());
                } else if (this.formatType.equals("ACM")) {
                    System.out.println(citation.getAuthorsACM());
                }
            }
        }
1
for(Citation c:citationIndex){
    if(c!=null){
    //...code here
    }
}

for (int i = 0; i < citationIndex.size(); i++) {
    if (citationIndex.get(i) != null) {
        //...code here
    }
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.