android list集合遍历,Java list集合遍历操作崩溃问题
都在子线程动态修改正在进行遍历的list集合,下边是五中遍历的是否崩溃的情况
public class MainActivity extends AppCompatActivity { private TextView test; private TextView test_2; public List<Person> mPersonList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test = findViewById(R.id.test); test_2 = findViewById(R.id.test_2); test.setOnClickListener(v -> { new Thread(new Runnable() { @Override public void run() { Person personNew = new Person(); personNew.age = 1; personNew.name = "haha"; mPersonList.add(personNew); Log.e("zzt_log", "增加了一个,size"+mPersonList.size()); } }).start(); }); test_2.setOnClickListener(v -> { new Thread(new Runnable() { @Override public void run() { searchList1(); // searchList2(); // searchList3(); // searchList4(); // searchList5(); } }).start(); }); for (int i = 0; i < 10; i++) { Person personNew = new Person(); personNew.age = i; personNew.name = "haha"; mPersonList.add(personNew); } } /** * TODO for i 的形式,动态修改list集合大小 * 结果:不会崩溃 * list大小会改变(循环次数会随着增多或减少),不会崩溃!! */ public synchronized void searchList1(){ for (int i=0; i< mPersonList.size(); i++){ Person person = mPersonList.get(i); Log.e("zzt_log", "i===="+i+", size"+mPersonList.size()+", age:"+person.getAge()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * TODO foreach 的形式,动态修改list集合大小 * 结果:崩溃!! * 底层就是iterator,所以同iterator情况一致 */ public synchronized void searchList2(){ int i = 0; for (Person person:mPersonList){ i++; Log.e("zzt_log", "i===="+i+", size"+mPersonList.size()+", age:"+person.getAge()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * TODO foreach + copy 的形式,动态修改list集合大小 * 结果:不会崩溃!! * mPersonList大小会改变 * mPersonListCopy大小不会改变 */ public synchronized void searchList3(){ int i = 0; List<Person> mPersonListCopy = new ArrayList(mPersonList); for (Person person:mPersonListCopy){ i++; Log.e("zzt_log", "i===="+i+", size"+mPersonListCopy.size()+", age:"+person.getAge()+", mPersonList size:"+mPersonList.size()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * TODO iterator的形式,动态修改list集合大小 * 结果:崩溃!! */ public synchronized void searchList4(){ Iterator<Person> iterators = mPersonList.iterator(); while (iterators.hasNext()) { Person session = iterators.next(); if (session.getAge() == 5) { iterators.remove(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.e("zzt_log", "while size"+mPersonList.size()); } } /** * TODO iterator + copy 的形式,动态修改list集合大小 * 结果:不会崩溃!! * mPersonList大小会改变 * mPersonListCopy大小不会改变 */ public synchronized void searchList5(){ List<Person> mPersonListCopy = new ArrayList(mPersonList); Iterator<Person> iterators = mPersonListCopy.iterator(); while (iterators.hasNext()) { Person session = iterators.next(); if (session.getAge() == 5) { iterators.remove(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.e("zzt_log", "mPersonListCopy size"+mPersonListCopy.size()+", mPersonList size:"+mPersonList.size()); } } }
posted on 2022-01-26 11:00 loveDoDream_zzt 阅读(246) 评论(0) 编辑 收藏 举报