• Post Reply Bookmark Topic Watch Topic
  • New Topic

weird behavior with array list of lists  RSS feed

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,,
I have an array of integers {2,3,9,8,7} and my purpose is to create all possible sets of solutions except the empty set.
For example, I would like to output for a set that contains the following sets

[[2],[2,3],[2,3,9],[2,3,9,8],[2,3,9,8,7]]

this is my code:

import java.util.*;

public class SubArrays
{
public static void main(String[] args)
{
int[] mainArr = new int[5];
mainArr[0] = 2;
mainArr[1] = 3;
mainArr[2] = 9;
mainArr[3] = 8;
mainArr[4] = 7;

for (int i=0 ; i < mainArr.length; i++)
System.out.println(mainArr[i]);


ArrayList<Integer> intList= new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> listOfintList = new ArrayList<ArrayList<Integer>>();

for (int i = 0; i < mainArr.length; i++)
{
Integer temp = new Integer(mainArr[i]);
intList.add(temp);
listOfintList.add(intList);
System.out.println(listOfintList);
}

}
}

this is the output which contradicts with intention [[2],[2,3],[2,3,9],[2,3,9,8],[2,3,9,8,7]]
the output:
[[2]]
[[2, 3], [2, 3]]
[[2, 3, 9], [2, 3, 9], [2, 3, 9]]
[[2, 3, 9, 8], [2, 3, 9, 8], [2, 3, 9, 8], [2, 3, 9, 8]]
[[2, 3, 9, 8, 7], [2, 3, 9, 8, 7], [2, 3, 9, 8, 7], [2, 3, 9, 8, 7], [2, 3, 9, 8, 7]]

I don't know why it is repeating for every step the number of times as the list element!!
please help!!
 
J P Wire
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this



The resulting output is:
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J P Wire wrote:Try this

The resulting output is:


Thank you so much J P Wire for replying!!
My purpose is not to print the content of the list but each time!!
I want to make my list to have other lists that contain the elements of the mainArr in this form:
[[2],[2,3],[2,3,9],[2,3,9,8],[2,3,9,8]]

I really appreciate your help!
Mona
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a correction:

My purpose is not to print the content of the list each time!!
I want to make a big list that contains other lists of the elements of the mainArr in this form:
[[2],[2,3],[2,3,9],[2,3,9,8],[2,3,9,8]]

because I want to find the size of the biggest list. In other words, how many lists elements that it can take from the mainArr!

I hope it is clear now!!

Thanks again!
I appreciate any help!
 
Koushik Ghosh
Greenhorn
Posts: 25
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just add the ArrayList each time into another ArrayList in the loop... :/
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koushik Ghosh wrote:just add the ArrayList each time into another ArrayList in the loop... :/




This is what I'm doing but the problem is when I print the listOfintList , it gives me repeated elements !!
I don't why it is repeating !!
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mona,

Try this once

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Mona,

Try this once



Thank you Swastik very much!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, please do not give out complete solutions. It is not helpful.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here, i was still trying to understand the question to work out my own solution and lo behold the solution is posted...... not good... :_(
Thanks Jeff, that is what's needed.
Just to re-iterate;
Please don't post the solutions to the problems for it simply annihilates the learning process of novices like me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!