如何将幂集的内容保存到Java的二维数组中

huangapple go评论113阅读模式
英文:

How to save the contents of a powerSet into a 2d array in Java

问题

我试图将从一维数组获得的PowerSet的内容保存到二维数组中。我尝试在“if”语句内部分配数组中的值,但是索引完全错误。

  1. int[] set = new int[]{2,4,5,8};
  2. int powSetLength = (int) Math.pow(2, set.length);
  3. int[][] powSet = new int[powSetLength][];
  4. for (int i = 0; i < powSetLength; i++) {
  5. for (int j = 0; j < set.length; j++) {
  6. if ((i & (1 << j)) > 0) {
  7. powSet[i] = new int[] //这里需要与子集对应的长度
  8. powSet[i][j] = set[j]; //我知道这是错误的,但我的想法是将子集中的每个数字分配到二维数组中
  9. }
  10. }
  11. }
英文:

I'm trying to save the contents of a PowerSet, obtained from a 1d Array into a 2d Array. I tried assigning the values in the array inside the "if" Statement but I'm getting the indices completely wrong

  1. int[] set = new int[]{2,4,5,8}
  2. int powSetLength = (int) Math.pow(2,set.length);
  3. int[][] powSet = new int[powSetLength][];
  4. for (int i = 0; i&lt;powSetLength; i++){
  5. for (int j = 0; j&lt;set.length; j++){
  6. if ((i &amp; (1&lt;&lt;j))&gt;0) {
  7. powSet[i] = new int[] //here needs to be the length corresponding to the subset
  8. powSet[i][j] = set[j]; //I know this is wrong but my idea was to assign each number of a subset into the 2d array
  9. }
  10. }
  11. }

答案1

得分: 0

由于您的内部数组长度是可变的,您可能希望使用内部的 java.util.ArrayList<Integer>。类似这样:

  1. int[] set = new int[]{2,4,5,8};
  2. int powSetLength = (int) Math.pow(2,set.length);
  3. List<Integer>[] powSet = new List[powSetLength];
  4. for (int i = 0; i<powSetLength; i++){
  5. for (int j = 0; j<set.length; j++){
  6. if ((i & (1<<j))>0) {
  7. // If the `i`'th powerSet isn't initialized yet: create an empty ArrayList:
  8. if(powSet[i] == null)
  9. powSet[i] = new ArrayList<>();
  10. // And add the current set-value to the List:
  11. powSet[i].add(set[j]);
  12. }
  13. }
  14. }
  15. System.out.println(Arrays.toString(powSet));

之后,您的列表数组将包含以下幂集:

  1. [null, [2], [4], [2, 4], [5], [2, 5], [4, 5], [2, 4, 5], [8], [2, 8], [4, 8], [2, 4, 8], [5, 8], [2, 5, 8], [4, 5, 8], [2, 4, 5, 8]]

在线尝试

英文:

Since your inner array is of variable length, you might want to use an inner java.util.ArrayList&lt;Integer&gt; instead. Something like this:

  1. int[] set = new int[]{2,4,5,8};
  2. int powSetLength = (int) Math.pow(2,set.length);
  3. List&lt;Integer&gt;[] powSet = new List[powSetLength];
  4. for (int i = 0; i&lt;powSetLength; i++){
  5. for (int j = 0; j&lt;set.length; j++){
  6. if ((i &amp; (1&lt;&lt;j))&gt;0) {
  7. // If the `i`&#39;th powerSet isn&#39;t initialized yet: create an empty ArrayList:
  8. if(powSet[i] == null)
  9. powSet[i] = new ArrayList&lt;&gt;();
  10. // And add the current set-value to the List:
  11. powSet[i].add(set[j]);
  12. }
  13. }
  14. }
  15. System.out.println(Arrays.toString(powSet));

After which your array of Lists will contain the following powerset:

  1. [null, [2], [4], [2, 4], [5], [2, 5], [4, 5], [2, 4, 5], [8], [2, 8], [4, 8], [2, 4, 8], [5, 8], [2, 5, 8], [4, 5, 8], [2, 4, 5, 8]]

Try it online.

huangapple
  • 本文由 发表于 2020年4月8日 22:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61103715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定