如何获得2D阵列中每个元素的所有可能组合?

时间:2022-04-18 02:35:24

I have a 2D array
int[][] lists=new int[][]{{22, 23},{34, 35},{47, 15}};

我有一个2D数组int [] [] lists = new int [] [] {{22,23},{34,35},{47,15}};

I have to get all combinations of each elements in every rows, like this: int [][] result= {{22,34,47},{22,34,15},{22,35,47},{22,35,15},{23,34,47},{23,34,15},{23,35,47},{23,35,15}}

我必须得到每一行中每个元素的所有组合,如下所示:int [] [] result = {{22,34,47},{22,34,15},{22,35,47},{22 ,35,15},{23,34,47},{23,34,15},{23,35,47},{23,35,15}}

Then for each rows from the newest array I have to calculate average. For this I create a method average(int[]a). All I need is to get only those combinations with highest average. Because my initial array can have a large number of rows/columns, I'm trying to generate each combination and check if it's average is higher then memorate it. Here is my code, but obviously it doesn't work. Can someone help me?

然后,对于来自最新阵列的每一行,我必须计算平均值。为此我创建了一个方法平均值(int [] a)。我只需要获得平均值最高的组合。因为我的初始数组可以有大量的行/列,所以我试图生成每个组合并检查它的平均值是否更高然后记住它。这是我的代码,但显然它不起作用。有人能帮我吗?

public static int average(int[]a) 
{
    int sum=0;
    for(int i=0;i<a.length;i++)sum+=a[i];
    return sum/a.length;
}
public static void cartesian(int[][] lists, int[] values, int n) 
{
   int sum=0;
    List<List<Integer>> result = new ArrayList<List<Integer>>();
   if (n == lists.length) {
    if(average(values)>sum) {
       result.clear();
        result.add(Arrays.stream(values).boxed().collect(java.util.stream.Collectors.toList()));
      sum=average(values);}
  }
  else 
   {
      for(int i: lists[n]) {
       values[n] = i;
       cartesian(lists, values, n+1);
    }
   }
return result;
}
 public static void main(String[] args)
    {
      List<List<Integer>> result = cartesian(lists, new int[lists.length], 0);
      for(List<Integer> i: result)  System.out.println(i);
     }

1 个解决方案

#1


-1  

I think there are a couple errors of logic in your cartesian code. From what I can tell, that is the function that determines what array has the highest average. First of all, in the statement

我认为你的笛卡尔代码中存在一些逻辑错误。据我所知,这是确定哪个数组具有最高平均值的函数。首先,在声明中

int sum=0;
    List<List<Integer>> result = new ArrayList<List<Integer>>();
   if (n == lists.length) {
    if(average(values)>sum) {

The last line is irrelevant - you've defined sum so that if your values aren't negative the if statement will always be true. Also, later in the code,

最后一行是无关紧要的 - 你已经定义了sum,这样如果你的值不是负数,那么if语句将永远为真。此外,稍后在代码中,

else 
   {
      for(int i: lists[n]) {
       values[n] = i;
       cartesian(lists, values, n+1);
    }

I think you meant to put cartesian(lists, values, n+1) outside of the for loop. Here is how I would rewrite this code:

我认为你的意思是将笛卡尔(列表,值,n + 1)放在for循环之外。以下是我将如何重写此代码:

public static void cartesian(int[][] lists, int[] values, int n) 
{
   int sum=0;
   List<List<Integer>> result = new ArrayList<List<Integer>>();
   int[] totest = lists[n];
   if(average(totest) > average(values) || r.equals(null)) {
       result.clear();
       result.add(Arrays.stream(totest).boxed().collect(java.util.stream.Collectors.toList()));
      if(n != lists.length - 1){cartesian(lists, totest, n + 1)};
}
  else 
   {
       if(n != lists.length - 1){cartesian(lists, totest, n + 1)};
   }
return result;
}

#1


-1  

I think there are a couple errors of logic in your cartesian code. From what I can tell, that is the function that determines what array has the highest average. First of all, in the statement

我认为你的笛卡尔代码中存在一些逻辑错误。据我所知,这是确定哪个数组具有最高平均值的函数。首先,在声明中

int sum=0;
    List<List<Integer>> result = new ArrayList<List<Integer>>();
   if (n == lists.length) {
    if(average(values)>sum) {

The last line is irrelevant - you've defined sum so that if your values aren't negative the if statement will always be true. Also, later in the code,

最后一行是无关紧要的 - 你已经定义了sum,这样如果你的值不是负数,那么if语句将永远为真。此外,稍后在代码中,

else 
   {
      for(int i: lists[n]) {
       values[n] = i;
       cartesian(lists, values, n+1);
    }

I think you meant to put cartesian(lists, values, n+1) outside of the for loop. Here is how I would rewrite this code:

我认为你的意思是将笛卡尔(列表,值,n + 1)放在for循环之外。以下是我将如何重写此代码:

public static void cartesian(int[][] lists, int[] values, int n) 
{
   int sum=0;
   List<List<Integer>> result = new ArrayList<List<Integer>>();
   int[] totest = lists[n];
   if(average(totest) > average(values) || r.equals(null)) {
       result.clear();
       result.add(Arrays.stream(totest).boxed().collect(java.util.stream.Collectors.toList()));
      if(n != lists.length - 1){cartesian(lists, totest, n + 1)};
}
  else 
   {
       if(n != lists.length - 1){cartesian(lists, totest, n + 1)};
   }
return result;
}