[JAVA] You may not want to use the remove method in ArrayList very often

I crashed today so I'll leave it as a memo

Source code that crashes


fun releaseGroupItem(uri: Uri){

        var i = 0
        selectImageList.forEach {
            if (it.imageUri == uri){
                selectImageList.removeAt(i)
            }
            i++
        }

        val adapter = SelectImageFragmentAdapter(supportFragmentManager, selectImageList)
        select_image_pager.adapter = adapter
        indicator_default.setViewPager(select_image_pager)
}

This will cause a crash with java.util.ConcurrentModificationException when this method is called consecutively.

This is a well-known story in Java, and I'm usually addicted to it when I'm just starting programming. (I've written the code that causes this phenomenon after 2 years of Android ...)

~~ If the cause is ArrayList # remove, it will not be deleted considering the order of the list. It is nicely summarized below. ~~ (added below) http://programming-study.com/technology/java-list-remove/

Code that does not crash


fun releaseGroupItem(uri: Uri){

        val iterator = selectImageList.iterator()
        while (iterator.hasNext()){
            val removeUri = iterator.next()
            if (removeUri.imageUri == uri) {
                iterator.remove()
            }
        }

        val adapter = SelectImageFragmentAdapter(supportFragmentManager, selectImageList)
        select_image_pager.adapter = adapter
        indicator_default.setViewPager(select_image_pager)
    }

Use ʻiterator. This one seems to have better performance, so it may be better to basically use ʻiterator except that the element to be deleted is simple and only called once.

It's an empty article, but I'll leave it as a personal note.

* Addition

I told you in the comment section!

https://docs.oracle.com/javase/jp/8/docs/api/java/util/ArrayList.html

Iterator simply allows you to add and remove elements in the List in the for statement, but ArrayList throws an exception when you do that.

Thank you for teaching!

Recommended Posts

You may not want to use the remove method in ArrayList very often
When you want to use the method outside
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
If you want to recreate the instance in cloud9
How to use the getter / setter method (in object orientation)
I want to use the sanitize method other than View.
Use JLine when you want to handle keystrokes on the console character by character in Java
How to use the link_to method
How to use the include? method
How to use the form_with method
If you want to include the parent class in Lombok's @builder
I want to remove the top margin in Grouped UITableView (swift)
What to do when you want to know the source position where the method is defined in binding.pry
[Rails] How to use the map method
[Java] How to use the toString () method
I want to use @Autowired in Servlet
To you who were told "Don't use Stream API" in the field
I want you to use Enum # name () for the Key of SharedPreference
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
If you want to satisfy the test coverage of private methods in JUnit
I want you to use Scala as Better Java for the time being
I want to use arrow notation in Ruby
When you want to bind InputStream in JDBI3
Output of how to use the slice method
[Swift] Use nonzeroBitCount when you want popcnt in Swift
How to use the replace () method (Java Silver)
I want to get the value in Ruby
[Ruby basics] How to use the slice method
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Code to use when you want to process Json with only standard library in Java
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to embed any TraceId in the log
I want to use fish shell in Laradock too! !!
If hash [: a] [: b] [: c] = 0 in Ruby, I want you to extend it recursively even if the key does not exist.
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to use a little icon in Rails
If you want to use Mockito with Kotlin, use mockito-kotlin
[Swift] Use UserDefaults to save data in the app
I want to call the main method using reflection
When you want to dynamically replace Annotation in Java8
[Rough commentary] I want to marry the pluck method
Memo that transitions to the login screen if you are not logged in with devise
When you want to reflect the Master Branch information in the Current Branch you are currently working on
How to get the class name / method name running in Java
[Beginner] I want to modify the migration file-How to use rollback-
Use collection_select to pull down the data stored in Active_Hash
I want to set the conditions to be displayed in collection_check_boxes
What to do when Method not found in f: ajax
I want to use screen sharing on the login screen on Ubuntu 18
I want to expand the clickable part of the link_to method
[Android, Java] Convenient method to calculate the difference in days
How to create a placeholder part to use in the IN clause
I want to call a method and count the number
Things you often use when doing web development in Java
I want to use the Java 8 DateTime API slowly (now)
Create a method to return the tax rate in Java
When you want to change the MySQL password of docker-compose
I want to transition to the same screen in the saved state
Delegate is convenient to use when you want to reuse parts