Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将较大的集合(集合、数组、列表)拆分为较小的集合,并跟踪最后返回的集合 public集合getCommentCollection(){ commentCollection=movie.getCommentCollection(); 返回拆分((列表)commentCollection,4); } 公共集合拆分(列表,整数大小){ int numBatches=(list.size()/size)+1; 集合[]批次=新集合[numBatches]; 集合集合=注释集合; for(int index=0;indexsize?size:col.size(); 而(!完成){ 主添加(列子列表(startIndex,endIndex)); 如果(endIndex==列大小()){ 完成=正确; } 否则{ startIndex=endIndex; endIndex=col.size()>(endIndex+size)?(endIndex+size):col.size(); } } } 返回主机; }_Java_Arrays_List_Collections - Fatal编程技术网

在Java中将较大的集合(集合、数组、列表)拆分为较小的集合,并跟踪最后返回的集合 public集合getCommentCollection(){ commentCollection=movie.getCommentCollection(); 返回拆分((列表)commentCollection,4); } 公共集合拆分(列表,整数大小){ int numBatches=(list.size()/size)+1; 集合[]批次=新集合[numBatches]; 集合集合=注释集合; for(int index=0;indexsize?size:col.size(); 而(!完成){ 主添加(列子列表(startIndex,endIndex)); 如果(endIndex==列大小()){ 完成=正确; } 否则{ startIndex=endIndex; endIndex=col.size()>(endIndex+size)?(endIndex+size):col.size(); } } } 返回主机; }

在Java中将较大的集合(集合、数组、列表)拆分为较小的集合,并跟踪最后返回的集合 public集合getCommentCollection(){ commentCollection=movie.getCommentCollection(); 返回拆分((列表)commentCollection,4); } 公共集合拆分(列表,整数大小){ int numBatches=(list.size()/size)+1; 集合[]批次=新集合[numBatches]; 集合集合=注释集合; for(int index=0;indexsize?size:col.size(); 而(!完成){ 主添加(列子列表(startIndex,endIndex)); 如果(endIndex==列大小()){ 完成=正确; } 否则{ startIndex=endIndex; endIndex=col.size()>(endIndex+size)?(endIndex+size):col.size(); } } } 返回主机; },java,arrays,list,collections,Java,Arrays,List,Collections,我正在尝试将一个较大的集合拆分为较小的集合,具体取决于原始集合中的项目数量。然后在每次调用get方法时返回一个较小的集合,同时跟踪返回的较小集合。如何实现这一点?您可以使用Vector.remove(collection),例如: public Collection<Comment> getCommentCollection() { commentCollection = movie.getCommentCollection(); return split(

我正在尝试将一个较大的集合拆分为较小的集合,具体取决于原始集合中的项目数量。然后在每次调用get方法时返回一个较小的集合,同时跟踪返回的较小集合。如何实现这一点?

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

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;
 }
public集合getCommentCollection(){
commentCollection=movie.getCommentCollection();
向量组=新向量();
对于(Comment com:commentCollection){
group.add(com);
if(group.size()==4){
打破
}
}
movie.getCommentCollection().remove(commentCollection);
回收;
}

假设movie.getCommentCollection()也是一个向量

我不完全确定你在问什么。。。是否要在返回前从源
集合中删除前4项,以便下次调用该方法时获得下4项?如果是这样,您可以只使用
迭代器

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;
}
Iterator iter=commentCollection.Iterator();
while(iter.hasNext()&&group.size()<4){
group.add(iter.next());
iter.remove();
}

但是,这样做会破坏
电影
对象的注释集合(除非它每次都返回该集合的副本,在这种情况下,上述方法根本不起作用)。我猜你是在尝试做一些类似分页的事情,在这种情况下,我建议做一些不同的事情,比如一个大小为4的注释列表,并跟踪分区列表中的当前索引(页面)。

这很简单:只需使用Guava。如果我正确地理解了您想要什么,它就是这样做的。

也许我不理解这个问题,但这是列表的一部分:

Iterator<Comment> iter = commentCollection.iterator();
while (iter.hasNext() && group.size() < 4) {
  group.add(iter.next());
  iter.remove();
}
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";
}
列表子列表(int-fromIndex,int-toIndex)
返回此列表中指定的fromIndex(包含)和toIndex(独占)之间部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回的列表由此列表支持,因此返回列表中的非结构更改将反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作

此方法消除了显式范围操作的需要(通常存在于数组中)。通过传递子列表视图而不是整个列表,任何需要列表的操作都可以用作范围操作。例如,以下习惯用法从列表中删除一系列元素:

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

公共静态列表拆分(集合输入,整数大小){\n
List master=new ArrayList();
if(input!=null&&input.size()>0){
列表列=新数组列表(输入);
布尔完成=假;
int startIndex=0;
int endIndex=col.size()>size?size:col.size();
而(!完成){
主添加(列子列表(startIndex,endIndex));
如果(endIndex==列大小()){
完成=正确;
}
否则{
startIndex=endIndex;
endIndex=col.size()>(endIndex+size)?(endIndex+size):col.size();
}
}
}
返回主机;
}

您可以使用ArrayList构造函数创建一个单独的子列表,该子列表是原始列表的深度副本

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;
}
import java.util.ArrayList;
导入java.util.List;
课堂擦伤{
公共静态void main(字符串[]args){
最终列表父项=新的ArrayList();
母公司。添加(“一”);
母公司。添加(“两个”);
母公司。添加(“三”);
//在这里使用ArrayList构造函数
最终列表副本=新的ArrayList(父.子列表(0,2));
//修改新列表不会影响原始列表
复制。删除(0);
//产出:
//家长:[一,二,三]
//副本:[两份]
System.out.println(“父项:“+parent”);
System.out.println(“副本:+副本”);
}
}

这是我的实现。希望有帮助

要查看的依赖项集合utils和列表:

/**
*高效集合分区
*
*@param baseCollection要拆分的基本集合
*@param maxSize返回的每个子列表的最大元素大小
*@param balling返回的每个子列表是否需要大小平衡
*@返回子列表,其顺序基于基集合的迭代器实现
*@自2020年3月12日起
*/
公共静态列表分区(最终集合baseCollection、int maxSize、布尔平衡){
if(CollectionUtils.isEmpty(baseCollection)){
返回集合。emptyList();
}
int size=baseCollection.size()%maxSize==0?baseCollection.size()/maxSize:baseCollection.size()/maxSize+1;
如果(平衡){
maxSize=baseCollection.size()%size==0?baseCollection.size()/size:baseCollection.size()/size+1;
}
int fullElementSize=baseCollection.size()%size==0?size:baseCollection.size()%size;
列表结果=Lists.newArrayListWithExpectedSize(大小);
迭代器it=baseCollect
List<E> subList(int fromIndex, int toIndex)
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;
}
import java.util.ArrayList;
import java.util.List;

class Scratch {
    public static void main(String[] args) {
        final List<String> parent = new ArrayList<>();
        parent.add("One");
        parent.add("Two");
        parent.add("Three");

        // using the ArrayList constructor here
        final List<String> copy = new ArrayList<>(parent.subList(0, 2));
   
        // modifying the new list doesn't affect the original
        copy.remove(0);

        // outputs:
        // parent: [One, Two, Three]
        // copy:   [Two]
        System.out.println("parent: " + parent);
        System.out.println("copy:   " + copy);
    }
}
/**
 * efficient collection partition
 *
 * @param baseCollection base collection to split
 * @param maxSize max element size of each sublist returned
 * @param balancing whether each of sublists returned needs size balancing
 * @return list of sublists, whose order bases on the base collection's iterator implementation
 * @since 2020/03/12
 */
public static <T> List<List<T>> partition(final Collection<T> baseCollection, int maxSize, boolean balancing) {

    if (CollectionUtils.isEmpty(baseCollection)) {
        return Collections.emptyList();
    }

    int size = baseCollection.size() % maxSize == 0 ? baseCollection.size()/maxSize : baseCollection.size()/maxSize+1;
    if (balancing) {
        maxSize = baseCollection.size() % size == 0 ? baseCollection.size()/size : baseCollection.size()/size+1;
    }
    int fullElementSize = baseCollection.size() % size == 0 ? size : baseCollection.size() % size;

    List<List<T>> result = Lists.newArrayListWithExpectedSize(size);
    Iterator<T> it = baseCollection.iterator();
    for (int i = 0; i < size; i++) {
        if (balancing && i == fullElementSize) {
            maxSize--;
        }
        maxSize = Math.min(maxSize, baseCollection.size()-i*maxSize);

        List<T> subList = Lists.newArrayListWithExpectedSize(maxSize);
        for (int i1 = 0; i1 < maxSize; i1++) {
            if (it.hasNext()) {
                subList.add(it.next());
            } else {
                break;
            }
        }
        result.add(subList);
    }

    return result;
}