1
 public static ArrayList<ArrayList<Integer>> powerSet(ArrayList<Integer> originalList) {
 ArrayList<ArrayList<Integer>> sets = new ArrayList<>();
 ArrayList<Integer> list = new ArrayList<Integer>(originalList);
 int p=2;
 ArrayList<Integer> st=new ArrayList<>();
 while(p<=originalList.size())
 {
 for(int i=0;i<=originalList.size()-p;i++)
 {
 for(int j=0;j<p;j++)
 {
 st.add(list.get(i+j)); 
 }
 sets.add(st);// sets recieves a st that is empty which is not the case
 Iterator it=st.iterator();
 System.out.print("adding it to our sets ");
 while(it.hasNext())
 System.out.print(it.next()+" ");// prints the elements contained inside and proves that st is not empty
 System.out.println("");
 st.clear();
 }
 p+=1;
 }
 return sets;
 }

I have created above function to return the powerset of contiguous elements of a list and each set is of size greater than 2. But due to some unknown problem the list 'st' cannot be added to arraylist of arraylists 'sets' even though the arraylist 'st' prints out fine .

asked May 24, 2016 at 0:10
3
  • What does cannot be added mean ? Commented May 24, 2016 at 0:11
  • And what // unable to print? Commented May 24, 2016 at 0:13
  • i have made edits to the question for better clarity , please see the question again. Commented May 24, 2016 at 0:15

3 Answers 3

1

The problem is caused by the line sets.add(st); followed by calling 'st.clear()'. Since Java passes objects by reference, sets contains the exact object st. This means that any subsequent change to st will change the contents of sets. To avoid this problem, copy st to a new object before adding it to sets. Try replacing sets.add(st); with sets.add(new ArrayList<Integer>(st));

answered May 24, 2016 at 0:31

1 Comment

That solved my problem, can we say this was like shadow cloning ?
0

Try this...

public static ArrayList<ArrayList<Integer>> powerSet(ArrayList<Integer> originalList) {
 ArrayList<ArrayList<Integer>> sets = new ArrayList<>();
 int p=2;
 while(p<=originalList.size()) {
 for(int i=0;i<=originalList.size()-p;i++) {
 ArrayList<Integer> st=new ArrayList<>();
 for(int j=0;j<p;j++) {
 st.add(list.get(i+j));
 } sets.add(st);
 Iterator it=st.iterator();
 System.out.print("adding it to our sets ");
 while(it.hasNext())
 System.out.print(it.next()+" ");
 System.out.println("");
 }
 p++;
 }
 return sets;
}
answered May 24, 2016 at 0:26

Comments

0

I would implement it this way:

public static ArrayList< ArrayList<Integer> > powerSet( ArrayList<Integer> originalList ) {
 ArrayList< ArrayList<Integer> > sets = new ArrayList<>();
 int p=2;
 while( p <= originalList.size() ){
 ArrayList<Integer> st; 
 for( int i=0 ; i<=originalList.size()-p ; i++ ){
 for( int j=0;j<p;j++ ){
 st = new ArrayList();
 st.add( originalList.get(i+j) ); 
 }
 sets.add(st);// sets recieves a st that is empty which is not the case
 Iterator it=st.iterator();
 System.out.print("adding it to our sets ");
 while( it.hasNext() )
 System.out.print(it.next()+" ");// prints the elements contained inside and proves that st is not empty
 System.out.println("");
 st.clear();
 }
 p+=1;
 }
 return sets;
}
answered May 24, 2016 at 0:37

1 Comment

@JPichardo Your code won't serve the same goal as first of all: 1)each of my set will get over-written by a new value in the innermost for loop and I will get sets of only 1 length 2) it will ask me to initialize the 'st' as if in some case it doesn't go into the innermost for loop then I will get an error of an uninitialized set.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.