简体   繁体   English

我无法弄清楚代码,队列和Java中的某些部分

[英]I cant figure out some part in my code, queue and java

import java.util.*;

class Subsets {

 public static List<List<Integer>> findSubsets(int[] nums){
 List<List<Integer>> result = new ArrayList<>();

 Queue<List<Integer>> queue = new LinkedList<>();
 queue.add(new ArrayList<>()); // add empty set to queue
 result.add(new ArrayList<>()); //add empty set to result

 for(int i=0; i<nums.length; i++){

     while(!queue.isEmpty()){
        System.out.println("current result = " + result);

        List<Integer> temp = queue.poll();
        System.out.println("current temp = " + temp);

        System.out.println("before change temp, current result = " + result);

        temp.add(nums[i]);
        System.out.println(i + " add index i value to temp, i= " + temp);
        System.out.println("not yet add to result, current result = " + result);

        result.add(temp);
        System.out.println("after add temp to result, result = " + result);
      }

      //add all elements in result to queue
      int j=0;
      while(j < result.size()){
      queue.add(result.get(j));
      j++;
     }
  }
  return result;
}


 public static void main(String[] args) {
   List<List<Integer>> result = Subsets.findSubsets(new int[] { 1, 3 });
   System.out.println("Here is the list of subsets: " + result);
 }
}

and here is output of the code 这是代码的输出

current result = [[]]
current temp = []
before change temp, current result = [[]]
0 add index i value to temp, i= [1]
not yet add to result, current result = [[]]
after add temp to result, result = [[], [1]]


current result = [[], [1]]
current temp = []
before change temp, current result = [[], [1]]
1 add index i value to temp, i= [3]
not yet add to result, current result = [[3], [1]]
after add temp to result, result = [[3], [1], [3]]


current result = [[3], [1], [3]]
current temp = [1]
before change temp, current result = [[3], [1], [3]]
1 add index i value to temp, i= [1, 3]
not yet add to result, current result = [[3], [1, 3], [3]]
after add temp to result, result = [[3], [1, 3], [3], [1, 3]]
Here is the list of subsets: [[3], [1, 3], [3], [1, 3]]

I know this is kind of dirty code, but rather than just think better way, I need to understand some part I still can't figure it out. 我知道这是一种肮脏的代码,但我不只是想更好的方法,还需要了解一些我仍然无法弄清楚的部分。

This is code to get subsets of the given set. 这是获取给定集合子集的代码。 For example, when we are given {1,3} then output should be {}, {1}, {3}, {1,3} 例如,当给定{1,3}时,输出应为{},{1},{3},{1,3}

I try to solve this question to use queue, but my point is that you can see second paragraph of the result output 我尝试解决使用队列的问题,但我的观点是您可以看到结果输出的第二段

before change temp, current result = [[], [1]]
1 add index i value to temp, i= [3]
not yet add to result, current result = [[3], [1]]

You definitely can see the point when you look at my code, I do nothing on result, I just add new value to temp, but result suddenly changed. 当您查看我的代码时,您肯定可以看到要点,我对结果什么也不做,我只是向temp添加新值,但是结果突然改变了。

I am not able to figure out what I did wrong or I may have wrong basis on queue? 我无法弄清楚我做错了什么,或者队列中的依据可能不正确?

To fix the error you have, you must understand that you are adding the same list reference to the results and the queue multiple times and thus modifying those same lists over and over again. 要解决您所遇到的错误,您必须了解要多次向结果和队列添加相同的列表引用,从而一次又一次地修改这些相同的列表。

Changing queue.add(result.get(j)); 更改queue.add(result.get(j)); to queue.add(new ArrayList<>(result.get(j))); queue.add(new ArrayList<>(result.get(j))); , makes a new list that is a copy of the results list passed to it (not a reference to the same list like before). ,将创建一个新列表,该列表是传递给它的结果列表的副本(而不是像以前一样引用同一列表)。 Because it is now copied, modifications later on like temp.add(nums[i]); 由于现在已复制,因此稍后进行修改,例如temp.add(nums[i]); no longer modify the result's lists. 不再修改结果列表。

import java.util.*;

class Subsets {

 public static List<List<Integer>> findSubsets(int[] nums){
 List<List<Integer>> result = new ArrayList<>();

 Queue<List<Integer>> queue = new LinkedList<>();
 queue.add(new ArrayList<>()); // add empty set to queue
 result.add(new ArrayList<>()); //add empty set to result

 for(int i=0; i<nums.length; i++){

     while(!queue.isEmpty()){
        System.out.println("current result = " + result);

        List<Integer> temp = queue.poll();
        System.out.println("current temp = " + temp);

        System.out.println("before change temp, current result = " + result);

        temp.add(nums[i]);
        System.out.println(i + " add index i value to temp, i= " + temp);
        System.out.println("not yet add to result, current result = " + result);

        result.add(temp);
        System.out.println("after add temp to result, result = " + result);
      }

      //add copy of all elements in result to queue
      int j=0;
      while(j < result.size()){
      queue.add(new ArrayList<>(result.get(j)));
      j++;
     }
  }
  return result;
}


 public static void main(String[] args) {
   List<List<Integer>> result = Subsets.findSubsets(new int[] { 1, 3 });
   System.out.println("Here is the list of subsets: " + result);
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚我的逻辑错误Java - Cant figure out my logic error Java Java的初学者,在最后一个if-else语句上,我只是无法弄清楚“但不是两个”部分 - Beginner at Java, on the last if-else statement, i just cant figure out the “but not both ” part java.lang.ArrayIndexOutOfBoundsException:0-我无法弄清为什么代码超出范围 - java.lang.ArrayIndexOutOfBoundsException: 0 - I cant figure out why the code is running out of bounds 我不明白为什么我的 Java 代码出错 - I cannot figure out why my Java code is erroring 我无法在代码中找出“ Java InputMismatchException错误” - I can't figure out the “Java InputMismatchException errors” in my code 我不知道为什么我的下一个 If 语句只是作为我最后的手段! 爪哇 - i cant figure out why for my next If statements it just goes to my last resort! JAVA 当我运行我的代码时,我得到 (0, null) 并且无法弄清楚为什么 - When I run my code I get (0, null) and cant figure out why 这个正则表达式有些错误,我无法弄清楚 - Some thing is wrong in this regular expression and i cant figure it out 我的代码做错了什么? 翻译成 PigLatin 并无法弄清楚如何打印返回值 - What am I doing wrong with my code? Translating into PigLatin and cant figure out how to print returned value 我对基本java applet的第一次尝试失败了。我无法弄清楚为什么矩形不会移动 - My first attempt at a basic java applet failed. i cant figure out why the rectangle does not move
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM