In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what to pay attention to in Java, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Instead of performing remove/add operations on elements in the foreach loop, you can do remove/add in fori. Use Iterator for remove elements. If you operate concurrently, you need to lock the Iterator object. [reference: https://my.oschina.net/u/3955185/blog/4496726]
/ / positive example 1: List list = new ArrayList (); list.add ("1"); list.add ("2"); Iterator iterator = list.iterator (); while (iterator.hasNext ()) {String item = iterator.next (); if (condition for deleting an element) {iterator.remove ();}} / / positive example 2: for (int item0
< list.size();i++) { if (list.get(i)==2) { list.remove(i); }}//反例:for (String item : list) { if ("2".equals(item)) { list.remove(item); }} 原因:由于foreach底层是用iterator进行遍历,源代码如下: public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException (); Object [] elementData = ArrayList.this.elementData; if (I > = elementData.length) throw new ConcurrentModificationException (); cursor = I + 1; return (E) elementData [lastRet = I];}
Where checkForComodification () is implemented as follows:
Final void checkForComodification () {if (modCount! = expectedModCount) {throw new ConcurrentModificationException ();}}
As you can see from the source code, when modCount! = expectedModCount will throw an exception, after using the remove () operation of list, modCount will add 1, but expectedModCount will not add 1, that is, the two values are different, and an exception will be thrown. Using remove () of Iterator changes the value of expectedModCount, so you need to use this method for remove/add.
Public void remove () {if (lastRet < 0) {throw new IllegalStateException ();} checkForComodification (); try {ArrayList.this.remove (lastRet); cursor = lastRet; lastRet =-1; expectedModCount = modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException ();}}
If you use fori for remove/add, you will not take the checkForComodification () method, so you can also use fori for operation, as in example 2.
2. When comparing Integer, use equals, otherwise when the compared data is not between-128127, the comparison is not the value, but the reference of the object, and the comparison result is not the expected value.
What are the points for attention of Java? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.