9 Replies - 279 Views - Last Post: 04 May 2013 - 02:53 PM Rate Topic: -----

#1 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Iterate through a dynamically changing list

Posted 04 May 2013 - 12:29 PM

How would I do this I have an Arraylist which will be updated while the iterator is going through how would I deal with this any help would be appreciated
Is This A Good Question/Topic? 0
  • +

Replies To: Iterate through a dynamically changing list

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 12:36 PM

You can not modify the ArrayList when iterating through it, else you will get an ConcurrentModificationException
Synchronize the ArrayList if multiple threads are using it

What is the problem you are trying to solve anyway :) ? Like what purpose does the ArrayList have?
Was This Post Helpful? 1
  • +
  • -

#3 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 12:51 PM

I am doing a 2d bin packing problem i can iterate through my list of bins fine.But in the bins are piles of parcels.Piles needed to be created so when you add a new pile the iterator would throw the above exception CasiOo has mentioned. I have two for loops one that goes through all the parcels.Inside it one that goes through all the trolleys.

Trolleys contains an arraylist of piles.
Piles contains an arraylist of parcels.

Bins are packed based on the conditions of a parcel being added to a pile in a trolley that space for it
And that the parcel is added to a pile that is for the given customer etc.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 01:11 PM

Oh so it isn't a multi threaded environment?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 01:33 PM

Vector are synchronized ArrayList just for that purpose

P.S.
And even if you use an ArrayList, ConcurrentModificationException can only be thrown my its Iterator so don't use Iterator and loop the old way for(int i = 0; i < al.size(); ++i)
Was This Post Helpful? 2
  • +
  • -

#6 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 02:07 PM

How would I return the index of value in a list that contains a value from a get method
I know how you construct contains but would the code following be on the right lines towards this


if(list.contains(getHeight()){



}


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 02:15 PM

If you want the index, you can't use the contains() method
you will need to use indexOf(object) wihich return the index of the object or -1 if not found
Was This Post Helpful? 0
  • +
  • -

#8 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 02:22 PM

so say i wanted the index of the object in the list that contained a particular value how would I achieve this.Just replace object with the value i wanted ie say getHeight() would that return the index of the object in the list that contained the value from the get method.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 02:24 PM

yes if your objects belong to a class that implements the equals() method to select based on your criteria for equality
Was This Post Helpful? 0
  • +
  • -

#10 RourkeOkeffe  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 89
  • Joined: 27-October 12

Re: Iterate through a dynamically changing list

Posted 04 May 2013 - 02:53 PM

I fixed one issue by moving the iterator and changing to a vector really has helped much thanks to pbl

This post has been edited by RourkeOkeffe: 04 May 2013 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1