2011-03-07 22 views
16
public Collection<Comment> getCommentCollection() { 
    commentCollection = movie.getCommentCollection();  
    return split((List<Comment>) commentCollection, 4); 
} 

public Collection<Comment> split(List<Comment> list, int size){ 

    int numBatches = (list.size()/size) + 1; 
    Collection[] batches = new Collection[numBatches]; 
    Collection<Comment> set = commentCollection; 

    for(int index = 0; index < numBatches; index++) { 
     int count = index + 1; 
     int fromIndex = Math.max(((count - 1) * size), 0); 
     int toIndex = Math.min((count * size), list.size()); 
     batches[index] = list.subList(fromIndex, toIndex); 
     set = batches[index]; 
    } 

    return set; 
} 

嗨,我試圖將集合分成更小的集合,具體取決於「母親」集合,並在每次調用get方法時返回其中一個較小的集合(記錄它是哪一個集合),有人可以幫我一個忙嗎?
非常感謝。
伊格納西奧
如何獲取子集合表單集合Java(Collections,Arrays,List)並跟蹤返回的最後一個

+1

ArrayList的優選載體。 – 2011-03-07 16:54:11

+0

沒有問題的直接鏈接,但你應該使用別的不是Vector(ArrayList?)=> http:// stackoverflow。com/questions/300519/arraylist-vs-vectors-in-java-if-thread-safety-isnt-a-concern – reef 2011-03-07 16:55:03

+0

現在它是一個ArrayList。 – 2011-03-07 16:59:36

回答

3
private int runs = 0; 

public void setRunsOneMore() { 
    runs++; 
} 

    public void setRunsOneLess() { 
    runs--; 
} 

public Collection<Comment> getCommentCollection() { 
    commentCollection = movie.getCommentCollection(); 
    Collection[] com = split((List<Comment>) commentCollection,4); 
    try{ 
     return com[runs]; 
    } catch(ArrayIndexOutOfBoundsException e) { 
     runs = 0; 
     } 
    return com[runs]; 
} 

public Collection[] split(List<Comment> list, int size){ 

    int numBatches = (list.size()/size) + 1; 
    Collection[] batches = new Collection[numBatches]; 
    Collection<Comment> set = commentCollection; 

    for(int index = 0; index < numBatches; index++) { 
     int count = index + 1; 
     int fromIndex = Math.max(((count - 1) * size), 0); 
     int toIndex = Math.min((count * size), list.size()); 
     batches[index] = list.subList(fromIndex, toIndex); 
    } 

    return batches; 
} 

設置下一個&以前的按鈕動作

public String userNext() { 
    userReset(false); 
    getUserPagingInfo().nextPage(); 
    movieController.setRunsOneMore(); 
    return "user_movie_detail"; 
} 

public String userPrev() { 
    userReset(false); 
    getUserPagingInfo().previousPage(); 
    movieController.setRunsOneLess(); 
    return "user_movie_detail"; 
} 
0

您可以使用Vector.remove(集合),例如:

public Collection<Comment> getCommentCollection() { 
    commentCollection = movie.getCommentCollection(); 
    Vector<Comment> group = new Vector<Comment>(); 
    for (Comment com:commentCollection){ 
     group.add(com); 
    if(group.size() == 4){ 
     break; 
    } 
    } 
    movie.getCommentCollection().remove(commentCollection); 
    return commentCollection; 
} 

假設movie.getCommentCollection()也是一個向量

2

我不完全知道你的」重新詢問......你是否想在返回它們之前從源Collection中刪除前4項,以便下次調用該方法時獲得下一個4項?如果是這樣,你可以只使用Iterator

Iterator<Comment> iter = commentCollection.iterator(); 
while (iter.hasNext() && group.size() < 4) { 
    group.add(iter.next()); 
    iter.remove(); 
} 

通過這樣做,不過,你會被破壞的評論movie對象的集合(除非它返回每次該集合的一個副本,在這種情況下,以上根本不起作用)。我猜你正在嘗試做類似於分頁的操作,在這種情況下,我會建議做一些不同的操作,例如partitioning a List,其大小爲4,並跟蹤該分區列表中的當前索引(頁面)。

+0

那會怎麼樣?我從來沒有用戶分區,你能提供一個簡單的例子嗎? – 2011-03-07 22:46:06

16

這很簡單,目前 「運行」:只要使用Lists.partition()番石榴。如果我理解你想要的東西,那就是它的功能。

36

也許我不明白的問題,但是這是清單的一部分:

List<E> subList(int fromIndex, int toIndex) 

返回指定的fromIndex(包括)元素範圍,獨家之間的這份名單的一部分的視圖。 (如果fromIndex和toIndex相等,則返回的列表爲空。)返回的列表由此列表支持,因此返回列表中的非結構化更改將反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可選列表操作。

該方法消除了對顯式範圍操作(數組通常存在的那種操作)的需要。任何需要列表的操作都可以通過傳遞子列表視圖而不是整個列表來用作範圍操作。例如,下面的語句中刪除的範圍從一個列表中的元素:

list.subList(from, to).clear();

docs.oracle.com/javase/1.5.0/docs/api/java/util/List.html

+1

這是頭腦風暴,我已經與收藏多年,但我從來沒有注意到這種方法! – Flo 2015-11-06 13:31:17

1
public static <E extends Object> List<List<E>> split(Collection<E> input, int size) {\n 
    List<List<E>> master = new ArrayList<List<E>>(); 
    if (input != null && input.size() > 0) { 
     List<E> col = new ArrayList<E>(input); 
     boolean done = false; 
     int startIndex = 0; 
     int endIndex = col.size() > size ? size : col.size(); 
     while (!done) { 
      master.add(col.subList(startIndex, endIndex)); 
      if (endIndex == col.size()) { 
       done = true; 
      } 
      else { 
       startIndex = endIndex; 
       endIndex = col.size() > (endIndex + size) ? (endIndex + size) : col.size(); 
      } 
     } 
    } 
    return master; 
} 
+2

請解釋你的答案,而不僅僅是給它。 – ArtB 2012-11-20 21:39:35

相關問題