2014-11-28 10 views
0

배열을 사용하여 인용 색인을 만드는 인터페이스를 구현했습니다. 필자는 새로운 프로그래머이며 현재의 Array 구현을 ArrayList 구현으로 변환하는 방법을 배우고 자합니다. 지금까지 제 코드가 있습니다. 배열 대신 printCitationIndex() 메서드에서 ArrayList를 어떻게 사용합니까?배열을 ArrayList로 변환

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!"); 
    } 
} 

} 
+0

는'나는 당신의 질문을 이해한다면 제대로 갔지 (I)''로 크기는()'과'[i]는''와 .length' 교체하는 단지 문제입니다 :) – xpa1492

답변

1

그것은 사용하는 것이 가장 년대 강화를위한 루프 이것에 대한 (그것은 또한 배열에 대한 작동하므로 이전에 그것을 발견했다면 구문, 배열 및 목록과 동일하게했을 것이다)

그것은 그것 같이 보인다
 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 
    } 
} 
관련 문제