Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help in ArrayList of ArrayLists

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help in ArrayList of ArrayLists

    Here is my code to return all the possible subsets of a set.

    Now my question is why when we uncomment the 1. line then the code works fine

    But when we uncomment the 2. line it simply returns a empty set

    why?

    Please help me with the logic behind it.

    ********************************************Code** ***********************************************
    public class Solution {
    public ArrayList<ArrayList<Integer>> ans;

    public void calc(ArrayList<Integer> A,ArrayList<Integer> arr,int top){
    for(int i = top;i<A.size();i++){
    arr.add(A.get(i));
    ArrayList<Integer> temp = new ArrayList<>();
    for(int j = 0;j<arr.size();j++){
    temp.add(arr.get(j));
    }
    if(!ans.contains(temp)){
    ans.add(temp);
    }
    calc(A,arr,i+1);
    arr.remove(arr.size()-1);
    }
    }

    public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> A) {
    Collections.sort(A);
    ans = new ArrayList<>();
    ArrayList<Integer> temp = new ArrayList<>();
    // 1. ans.add(new ArrayList<>());
    // 2. ans.add(temp);
    calc(A,temp,0);
    return ans;


    }
    }

    ***************************************code******* *******************************************

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help in ArrayList of ArrayLists

    Please post the main method so the code can be compiled and executed for testing.

    The variable: ans should not be a class level variable. If methods require access to it, it should be passed to the method as an argument.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ArrayLists and Classes
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 28th, 2014, 04:48 PM
  2. PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.
    By javahelpneeded in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2014, 05:54 PM
  3. boolean arraylists?
    By Scorks in forum Collections and Generics
    Replies: 1
    Last Post: October 15th, 2013, 11:47 AM
  4. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  5. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM

Tags for this Thread