• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

ArrayListr logic getting into some trouble

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making a sound track / music library tracking software. Let's say the central server have a master list of all the songs and each user have their own personal list, the criteria is that a person's personal list cannot have anything that's not inside the master list. I wanted to create a method below that can show the users a list of music they don't have on their personal list yet and I call the method 'getAvMusicList'. It's the below codes.



I decided to make the dbList and usrDBLs the same but the placement of the item elements are different to simulate a situation where the master list and the user list are the same so there's no available list of songs to recommend to the user and it should return some sort of an empty list but instead it returned a list of songs that the user already have.

below is the runtime result:



Why did it miss the two music to be removed ?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be misunderstanding your question but test.mp3 and rock.mp3 were removed. Are you saying this is not what you expected to happen?
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Removing from a personal list should never influence the master list, but removing from the master list should influence the personal list. I think you should check out the bulk methods, in particularly retainAll.

Or, if possible, simply remove elements from the personal lists at the moment you remove from the master list.
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant that since the user's list of music is the same as the master list, all the music in the master list should be removed so that it can return an empty list to recommend the user (since there's nothing to recommend the user) but instead, it only removed 2 songs and left the other two songs still in the master list to attempt to recommend the user.
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then how about another one of the bulk methods: removeAll?
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You are getting into trouble because of dynamically changing ArrayList. When you remove an item from dbList it gets adjusted to the size of the remaining elements.Means its indexes are also getting adjusted. i.e why you are getting problem while iterating with for(int i .... ) . Instead you can use code as shown below

ListIterator dbIter=dbList.listIterator();
ListIterator userIter;
while(dbIter.hasNext())
{
checkingDB = (String)dbIter.next();
userIter=usrDBLs.listIterator();
while(userIter.hasNext())
{
checkingUsr = (String)(userIter.next());
if (checkingDB.equals(checkingUsr)) {
dbIter.remove();
break;
}
}
}


Hope this will definitely works for you. It took my precious two hours. Have a good time ahead.
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Here is the improved version for you.Its working well.


ListIterator dbIter=dbList.listIterator();
while(dbIter.hasNext())
{
checkingDB = (String)dbIter.next();
if(usrDBLs.contains(checkingDB))
{
dbIter.remove();
}
}
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As suested by rob this too works well.

dbList.removeAll(usrDBLs);
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic