• Post Reply Bookmark Topic Watch Topic
  • New Topic

listing of ArrayList<ArrayList<Integer>>  RSS feed

 
Miroslav Benko
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code:



if i want printout the listOfPermutation in main method,
System.out.println(nameOfClass. listOfPermutation);
listing is empty. Why?

output:


sory for english... thanks for answer
 
Mike Degteariov
Ranch Hand
Posts: 145
8
Java Mac MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Miroslav

Take a look at your if statement



Here, "where" has the value of 0, and "n" has the value of 3
So here 0 < 3 and thus "if" part executed, and "else" part does not.

So

never happens.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Miroslav,

well, there's not much wrong with your code, it is short and elegant.

The problem is: once you have a complete permutation, and store that
in the listOfPermutation, that's fine, but then you start to empty that same
permutation, in line 12.

However, that is the same permutation that is also referenced in your
listOfPermutation, so it gets emptied there as well.
And you see the result of that in your output.

The remedy is to create a new ArrayList<Integer>, put the contents
of permutation in it, and store that in the listOfPermutation.

For instance, at line 16 and a half, insert the code

and add 'flup' in line 17.

Greetz,
Piet
 
Miroslav Benko
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
For instance, at line 16 and a half, insert the code

and add 'flup' in line 17.

Greetz,
Piet


super now it works, thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!