• Post Reply Bookmark Topic Watch Topic
  • New Topic

List Permutation  RSS feed

 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im trying to solve this problem:
You have two list of numbers.
The size of the list is larger than 5, but smaller than 20. The numbers in the lists are not sorted; a number can appear more than once in a list. Examples of such lists are [1,2,3,4,5,6,7,8,9,10,11,12] and [1,2,3,5,7,9,11]
I am to write a class NumberListApplication that contains only a main method. The main method will construct two NumberList-objects and store the numbers from your lists into the two NumberList-objects. Then a third NumberList-object is constructed that contains the numbers from the first list that are not part of the second list.

Explanation: if the contents of the two lists are [1,2,3,4,5,6,7,8,9,10,11,12] and [1,2,3,5,7,9,11], the contents of the new list will be [4,6,8,10,12] or a permutation of this.

I have written the classes and they are fine..my problem lies in generating the third list. My major problems are:

1. I need to know the size of the new list to be generated..so that i can create a numberlist object..with the size of the list as a parameter.
2.I have problems with generating the third list...any ideas...sample code would really help.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You don't need to know the size in advance (unless you are in fact using arrays instead of Lists).

2. Take a look at Collection.removeAll

Also, what should the result be for the lists [42, 42] and [42]?
 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well firstly the list is a NumberList class that i wrote as part of the project.. So im not meant to use collections or stuff.
And for [42,42] and [42] well...thats actually one of the test cases for the project..
im just meant to construct a new list with numbers in the first list that arent in the second.
 
Daniel Zammit
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to know the size of the third list? You'd only need that if you were declaring it like a fixed size array. A List in Java doesn't have a fixed size and it allows you to add and remove elements on the fly.

There are several ways to do this, and the easiest one is through a double loop. In order to determine whether a number should be in the third list, you have to pick a number from one list and then walk through the other list to see if that number is there. If you find the number in the second list, then you know that the number should not be in the third list. If you don't, then you put that number into the third list. In Java, the List object has a contains() method that can be used to determine whether or not an element is in that list or not.
 
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im using a NumberList class that i wrote my self as part of an assignment.
The constructor is like this

NumberList(n); where n is the capacity of the new NumberList im creating. so i need to know what n is before i start adding elements to it.

As for the double loop.. i did that but im not getting the right results here it is:

i first decided to use arrays first then write the results to the numberlist

sizeA is the size of the first array..listA
sizeB is the size of the second array ...listB
my logic is if listA[i] equals listB[j] then break out of the loop since theres no point continuing
if not then print the value of listA[i] but im not getting the results i want, from this code.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!