Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 在动态洗牌列表中循环_Java_Sorting_Graph Algorithm - Fatal编程技术网

Java 在动态洗牌列表中循环

Java 在动态洗牌列表中循环,java,sorting,graph-algorithm,Java,Sorting,Graph Algorithm,这是一种情况:给定两个列表[A,B,C,D]和[Ob,Od,Oa,Oc],对象O有一个参数A,B,C或D 首先列出的是根据特定条件排序的。如果a是产品,它是最便宜的,D是最贵的。我们希望以这种方式执行2-for循环: List products = [A,B,C,D]; List shops = [Ob,Od,Oa,Oc]; int k=0; for(Object i : products) for(Object o : shops) // 1st loop i is A

这是一种情况:给定两个列表[A,B,C,D]和[Ob,Od,Oa,Oc],对象O有一个参数A,B,C或D

首先列出的是根据特定条件排序的。如果a是产品,它是最便宜的,D是最贵的。我们希望以这种方式执行2-for循环:

List products = [A,B,C,D];
List shops = [Ob,Od,Oa,Oc];
int k=0;
for(Object i : products)
    for(Object o : shops)
        // 1st loop i is A, and o is Ob
        consume(A,Ob) // We are reducing number of A depending on Ob requirement
        k++; // 1st loop k = 0;
对于Ob,我们知道ab比a好。因此,在第一个循环中,我想在a和B之间切换,所以我的列表变成[B,a,C,D]

List products = [B,A,C,D]; // during the first loop, this list was shuffled.    
List shops = [Ob,Od,Oa,Oc];
int k=0;
for(Object i : products)
    for(Object o : shops)
        // 2nd loop i is B, and o is Od --> B because we shifted
        consume(A,Ob) // We are reducing number of A depending on Ob requirement
        k++; // 2nd loop k = 1;
在最后一个循环中,我们应该有[B,D,A,C]

当然,在一个并行系统中这样做是可行的,每个列表[a,B,C,D]的组合有一个线程,然后返回最优解,但很困难

Java中是否有任何东西可以洗牌我们正在迭代的列表?

在开始迭代之前,我会调用一次。Collection.shuffleist执行随机洗牌。而在我的例子中,洗牌取决于条件。