Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Need Java Help Java, Arraylist and removal

Name: Anonymous 2012-01-18 1:38

Every time I try to call ArrayList.remove() on an element while in a for each loop, Java throws a ConcurrentModificationException.  I need to remove an element in a for loop because in there is where I look through the objects to find objects which meet the criteria for removal.

Name: Anonymous 2012-01-18 1:48

Found a retarded workaround:

    public void updateState() {
        for(Renderable renderable:renderables){
            renderable.updateState();
            if(renderable.markedForDeletion){
                this.markedForDeletion.add(renderable);
            }
        }
        if(!markedForDeletion.isEmpty()){
            renderables.removeAll(markedForDeletion);
            markedForDeletion.removeAll(markedForDeletion);
        }
    }

Name: Anonymous 2012-01-18 1:51

it would not let me do this:

public void updateState() {
        for(Renderable renderable:renderables){
            renderable.updateState();
            if(renderable.markedForDeletion){
                renderables.remove(renderable);
            }
        }
    }

threw the exception when tried to remove in this manner

Name: Anonymous 2012-01-18 2:19

>>3

Maybe using a lower level form of iteration will solve the problem? Removing elements from a list or array or whatever while iterating over it can be tricky depending on the underlying representation, so maybe Java doesn't know how to do it and you just need to get more specific so you can do it correctly yourself?

Name: Anonymous 2012-01-18 4:51

Alternatively, use an iterator.


Iterator<Renderable> it = renderables.iterator();
while(it.hasNext()) {
    Renderable renderable = it.next();
    renderable.updateState();

    if(renderable.markedForDeletion()) {
        it.remove();
    }
}

Name: Anonymous 2012-01-18 5:24

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will thow this exception.

>>5
(unchecked)Seems like a reasonable solution.

Name: Anonymous 2012-01-18 5:36

use Vectors
it's thread safe

Name: Anonymous 2012-01-18 6:30

It is common for Iterators in Java to be invalid if the underlying data structure is changed. Since it is an arraylist you can use a regular for-loop and get/remove. Just remember to decrement the index after removing an element.

Name: Anonymous 2012-01-18 9:03

>>7
This has nothing to do with thread-safe problems. This is due to a moron who doesn't understand what the sugar syntactic interator for loop :

        for(Renderable renderable:renderables){


requires from its user.

Name: Anonymous 2012-01-18 9:21

DUBS INCOMING

Name: Anonymous 2012-01-18 9:21

VENI VEDI VICI  <----

Name: Anonymous 2012-01-18 11:19

>>5
Thanks, I will try this.

>>9
cool story bro

Name: Anonymous 2012-01-18 11:23

>>12
You should learn what the syntax does before you go off using it and then come whining to us about why it doesn't work.


fucking java programmers...

Name: Anonymous 2012-01-18 11:25

>>1
Fuck off back to your AbstractCubicleFactoryFactory, ``faggot"

Name: Anonymous 2012-01-18 11:33

>>13
Go back to your own country you foreign piece of shit.

Name: Anonymous 2012-01-18 11:34

>>13
>>14
Actually I'm not a Java programmer, just an embedded systems designer who  makes more than you ever will.  The only reason I'm using java is because my friend asked me to help him write a game engine for android.  I don't even code as a profession anymore, I design... I leave the coding to the slaves and only code when they fuck up big time.

Name: Anonymous 2012-01-18 11:37

GC is shit.

Name: Anonymous 2012-01-18 11:39

>>16

>>13 is a toilet scrubber. Seriously. Based on some of his past posts, this idiot isn't capable of making a correct statement about programming. On top of that, his written communication skills suck.

Name: Anonymous 2012-01-18 11:52

>>16
>>18
both of you are morons who can't code properly. Go back to your fantasy lives where you dream that you're as successful as kodak master programmer.

Name: Anonymous 2012-01-18 11:57

>>19
Again, you're more than welcome to waddle over to ste #300 on 64rd and Hollis and tell me that.

Name: Anonymous 2012-01-18 11:59

>>19
Also you idiot, in case you haven't noticed, I have a different writing style than >>16. Again, you're stupid. And again, you have no possible future as a comoputer programmer.

Name: Anonymous 2012-01-18 12:26

comoputer
he mad ololo

Name: Anonymous 2012-01-18 12:59

>>20
If you has any good reading comphension you would notice that I stated that they are jealous of your enterprise java job and wish they were like you.

Please do not make my make my Kodak-san look like a moron.

Name: Anonymous 2012-01-18 13:35

Kodak is now a meme

Name: Anonymous 2012-01-18 13:40

>>1
you cannot remove elements of an array during a for each
use a manual while loop and SAVE where you are because removing an element during traversal fucks things up

Name: Anonymous 2012-01-18 13:51

>>25
But you can remove the data in that element.

Name: Anonymous 2012-01-18 14:11


for each in java is absolute shit, stop using it.

Instead consider a proper Iterator or a basic for loop.


/* THE LOGICAL WAY */
Object o;
int i;
for(i=0;i<anus.size();++i){
  /* BULLSHIT */
  if(I_SHOULD_REMOVE_THIS_INDEX){
    o = anus.remove(i);
    --i;
    /* BULLSHIT WITH O */
  }
  /* BULLSHIT */
}

Name: Anonymous 2012-01-18 14:55

>>27
The joys of not having first-class procedures to write per-data structure map and reduce methods/functions.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-18 14:58

>>28
I think you're talking out of your ass again. Convince me otherwise.

Name: Anonymous 2012-01-18 15:08

>>28
wtf are you trying to get at.

Name: Anonymous 2012-01-18 15:17

Use an array instead of an ArrayList.

Name: Anonymous 2012-01-18 15:20

(remove-if <your-condition-here> anus)

Lisp is shit.

Name: Anonymous 2012-01-18 15:22

>>31
An ArrayList is a array at its heart.

Java's ArrayList is full of bloated unneeded error handling though.

Use this ArrayList implementation instead:

/* Program left as an exercise to the reader */

Name: Anonymous 2012-01-18 15:36

>>33
nice dubs

Name: Anonymous 2012-01-18 15:40

>>34
fuck off

Name: Anonymous 2012-01-18 15:42

>>31,33
http://www.docjar.com/html/api/java/util/ArrayList.java.html
Since Java arrays are fixed-size, they would eventually need to have a new array created and the objects copied into the new array using System.arraycopy. Internally, a Java ArrayList isn't much different than a C++ std::vector or a C array expanded using realloc (except for the bloated unneeded error handling).

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-18 16:59

>>36
An array and an ArrayList are two different things in Java. Also, an array in C *cannot* be expanded using realloc(). Holy cripes, please shut up and read a book.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-18 17:00

>>30
That once again you are speaking about something that you have next to no clue about.

Name: Anonymous 2012-01-18 17:52

>>36
How is error handling bloat? Have you considered that the errors might actually happen?

Name: Anonymous 2012-01-18 19:06

>>39
Probably he meant there are redundant checks.
I didn't check the code since it's too puke-inducing, so I'll just assume that.

Name: Not >>28 2012-01-18 19:44

>>29
>>28 meant the third fucking thing you learn about functional programming after learning about basic data types and how to define functions.

Read SICP.

Name: Anonymous 2012-01-18 20:08

Name: Anonymous 2012-01-18 20:09

ENTERPRISE COMMENTING BY JOHN BLOCH AND NEAL FAGTER


   93    * @author  Josh Bloch
   94    * @author  Neal Gafter
  884       /**
  885        * Returns a view of the portion of this list between the specified
  886        * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If
  887        * {@code fromIndex} and {@code toIndex} are equal, the returned list is
  888        * empty.)  The returned list is backed by this list, so non-structural
  889        * changes in the returned list are reflected in this list, and vice-versa.
  890        * The returned list supports all of the optional list operations.
  891        *
  892        * <p>This method eliminates the need for explicit range operations (of
  893        * the sort that commonly exist for arrays).  Any operation that expects
  894        * a list can be used as a range operation by passing a subList view
  895        * instead of a whole list.  For example, the following idiom
  896        * removes a range of elements from a list:
  897        * <pre>
  898        *      list.subList(from, to).clear();
  899        * </pre>
  900        * Similar idioms may be constructed for {@link #indexOf(Object)} and
  901        * {@link #lastIndexOf(Object)}, and all of the algorithms in the
  902        * {@link Collections} class can be applied to a subList.
  903        *
  904        * <p>The semantics of the list returned by this method become undefined if
  905        * the backing list (i.e., this list) is <i>structurally modified</i> in
  906        * any way other than via the returned list.  (Structural modifications are
  907        * those that change the size of this list, or otherwise perturb it in such
  908        * a fashion that iterations in progress may yield incorrect results.)
  909        *
  910        * @throws IndexOutOfBoundsException {@inheritDoc}
  911        * @throws IllegalArgumentException {@inheritDoc}
  912        */
  913       public List<E> subList(int fromIndex, int toIndex) {
  914           subListRangeCheck(fromIndex, toIndex, size);
  915           return new SubList(this, 0, fromIndex, toIndex);
  916       }

Name: Anonymous 2012-01-18 20:11

>>37
Also, an array in C *cannot* be expanded using realloc(). Holy cripes, please shut up and read a book.

I think you took his statement more literal then it was meant to be taken. You have a bad day at work like you seem to always do? Enterprise java getting you your head after all these years ?

Name: Anonymous 2012-01-18 20:12

Most them are Javadocs that will appear outside.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-18 20:16

>>44
Well, maybe if you were a bit more clear and concise with your technical writing, there wouldn't be such confusion.

Name: Anonymous 2012-01-18 20:16

>>45
They are all Javadocs

Name: Anonymous 2012-01-18 20:19

>>46
if you were

There's another thing, you need to realize there's more than 2 people on this board ( you and me ). I'm not the one that originally wrote that post but i at least clearly understand what he was getting. Please do not accuse me of such horrible travesties.




Now, lets talk about your anger issues you appear to be having. Take the next 10 minutes to write down a list of all the things that anger you in the world and post it to share with the group. We're here to help you.

Name: Anonymous 2012-01-18 20:28

>>48
1)Ex girlfriends who posts pictures of me "being her bitch" on myspace.

2)The neighbors dog. I swear if I hear that thing bark one more time at 3am, I'm gonna throw an m80 over the fence, and then call 911 to report that I heard a gunshot next door.

3)Carrots

4)Theo da Raadt from the OpenBSD project

Name: Anonymous 2012-01-18 20:30

>>49'
>2012
>using myspace



wtfamireading.jpg

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-18 20:34

>>48
My anger issues are mild compared to the following guy that I used to work with...

http://www.sysarch.com/resumes/resume.html

Name: Anonymous 2012-01-18 20:42

>>51
Did the guy just love to go from job to job or was he getting fired?

Name: Anonymous 2012-01-18 20:44

>>52
Do you know what a consultant does?

Name: Anonymous 2012-01-18 20:45

>>53
I guess it would have been smarter of me to actually look at his job title for all those jobs

Name: Anonymous 2012-01-18 20:54

>>51
Now all we have to do is contact him and see if he remembers any man that would ramble on about mental midget toilet scrubbers and we'll find out who you truly are.

Name: Anonymous 2012-01-18 21:01

>>55
Be sure to ask him about the penis extension that he got for "perl girl".

Name: Anonymous 2012-01-18 23:26

>>39

error checking is bloat when errors should never happen, and errors should never happen, but sometimes they do, and if there is checking, then you are less fucked than if errors were checked, but you are stilled fucked, but the fucking up only prevents the functioning of your program, rather than causing it to do something completely arbitrary, or intentional from a hacker who has learned how to exploit the error, so sometimes it is worth it to have the error checking, but if the application is proved correct and if speed is needed, then error checking can get in teh way of getting adequate speed.

Name: Anonymous 2012-01-18 23:30

>>57
I have no idea what your message is. Try writing in English.

Name: Anonymous 2012-01-18 23:35

***than if errors were not checked,***

Name: Anonymous 2012-01-18 23:37

>>58
you should considering be open to read the other langaugeses.

Name: Anonymous 2012-01-19 0:09

>>58
You should consider learning how to learn English.

You're being very butthurt when someone comes along and tells you why something is worthless.

Name: Anonymous 2012-01-19 1:23

>>61
Implying I don't learn English
Implying I am Hutt the Bert

Don't change these.
Name: Email:
Entire Thread Thread List